<?php
namespace app\api\controller;

use Phinx\Db\Adapter\PostgresAdapter;

/**
 * @SWG\Swagger(
 * schemes={"https","http"},
 * host="wx.hncegbim.com/api/Hunanconstr",
 * consumes={"multipart/form-data"},
 * produces={"application/json"},
 * @SWG\Info(
 * version="1.0",
 * title="湖南建工",
 * description="湖南建工接口文档<br> 以后就在这里愉快的对接口把!<br>"
 * ),
 *
 * @SWG\Tag(
 * name="Ranking",
 * description="排行榜模块",
 * ),
 *
 * @SWG\Tag(
 * name="Project",
 * description="项目模块",
 * ),
 *
 * @SWG\Tag(
 * name="Bound",
 * description="绑定模块",
 * ),
 *
 * @SWG\Tag(
 * name="User",
 * description="用户模块",
 * ),
 *
 * @SWG\Tag(
 * name="index",
 * description="首页模块",
 * ),
 *
 * @SWG\Tag(
 * name="Region",
 * description="地区模块",
 * ),
 *
 * @SWG\Tag(
 * name="Brush",
 * description="刷刷模块",
 * )，
 *
 * @SWG\Tag(
 * name="Center",
 * description="个人中心模块",
 * )
 * )
 */
class Hunanconstr extends Base
{

    /*
     * 获取个人信息
     * 判断个人中心是否查得到信息，否者去微信user里面拿
     */
    public function UserName($user_id)
    {
        $res = db('wx_jg_personal')->alias('a')
            ->join('applet_user b', 'a.user_id = b.id')
            ->field('a.name,b.avatarurl')
            ->where([
            'a.user_id' => $user_id
        ])
            ->find();
        if (! $res) {
            $res = db('applet_user')->field('nickname as name,avatarurl')
                ->where([
                'id' => $user_id
            ])
                ->find();
        }
        return ($res);
    }

    /**
     * 获取用户绑定项目ID
     */
    public function MyBound($user_id)
    {
        $res = db('wx_jg_bound')->field('item_id')
            ->where([
            'user_id' => $user_id
        ])
            ->select();
        $arr = [];
        foreach ($res as $key => $val) {
            $arr[$key] = $val['item_id'];
        }
        return $arr;
    }

    /**
     * 获取用户关注项目ID
     */
    public function MyFollow($user_id)
    {
        $res = db('wx_jg_follow')->field('item_id')
            ->where([
            'user_id' => $user_id
        ])
            ->select();
        $arr = [];
        foreach ($res as $key => $val) {
            $arr[$key] = $val['item_id'];
        }
        return $arr;
    }

    /*
     * 根据项目ID获取项目名称
     */
    public function ProjectName($item_id)
    {
        $res = db('project')->field('project_name')
            ->where([
            'id' => $item_id
        ])
            ->find();
        return ($res);
    }

    /*
     * 获取多久之前
     */
    function format_date($time)
    {
        $t = time() - $time;
        $f = array(
            '31536000' => '年',
            '2592000' => '个月',
            '604800' => '星期',
            '86400' => '天',
            '3600' => '小时',
            '60' => '分钟',
            '1' => '秒'
        );
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int) $k)) {
                return $c . $v . '前';
            }
        }
    }

    /*
     * 获取发布时间
     */
    public function NewTime($create_time)
    {
        // 上传多长时间
        $NewDay = floor((time() - $create_time) / 86400); // 天
        $NewH = floor(((time() - $create_time) - ($NewDay * 86400)) / 3600); // 时
        $NewM = floor(((time() - $create_time) - ($NewH * 3600)) / 3600); // 分
        
        if ($NewDay == 0) {
            $NewTime = $NewH . "小时" . $NewM . "分钟前";
        } elseif ($NewDay == 0 and $NewH == 0) {
            $NewTime = $NewM . "分钟前";
        } elseif ($NewDay == 0 and $NewH == 0 and $NewM == 0) {
            $NewTime = "当前";
        } else {
            $NewTime = $NewDay . "天" . $NewH . "小时" . $NewM . "分钟前";
        }
        return ($NewTime);
    }

    /**
     * @SWG\Post(
     * path="/is_Permissions",
     * tags={"User"},
     * summary="判断是否有权限",
     * description="判断是否有权限 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="is_Permissions", type="integer", description="是否有权限({0=无,1=有})"),
     * )
     * )
     */
    public function is_Permissions($user_id = '', $project_id = '')
    {
        // 接收数据
        if (empty($user_id)) {
            $user_id = $this->accessData['uid'];
        }
        if (empty($project_id)) {
            $project_id = input('id'); // 项目id
        }
        // 接收数据判断区
        if (empty($user_id)) {
            err('user_id NOT NULL');
        }
        if (empty($project_id)) {
            err('项目id NOT NULL');
        }
        
        // 查询权限
        $is_Permissions = db('wx_jg_bound')->where("user_id='{$user_id}' and item_id='{$project_id}'")->find();
        
        // 判断数据是否为空
        if ($is_Permissions) {
            return json_encode([
                'is_Permissions' => '1',
                'Permissions' => $is_Permissions
            ]);
        } else {
            return json_encode([
                'is_Permissions' => '0'
            ]);
        }
    }

    /**
     * @SWG\Post(
     * path="/index",
     * tags={"index"},
     * summary="首页",
     * description="首页-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="分页参数-页码", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="pagesize", description="分页参数-页数", in="formData", required=false, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="company-id", type="integer", description="用户ID"),
     * @SWG\Header(header="company-name", type="string", description="公司名称"),
     * @SWG\Header(header="company-number", type="string", description="项目数量"),
     * @SWG\Header(header="item-id", type="integer", description="项目id"),
     * @SWG\Header(header="item-project_name", type="string", description="项目名称"),
     * @SWG\Header(header="item-project_model", type="string", description="项目模型图")
     * )
     * )
     */
    public function index()
    {

        $a=1;
        $b=100;
        if ($a == $b){
            echo 111;
        }else{
            echo 222;die;
        }
die;
        $b = db('cmf_test_wx_jg_album')->field('create_time,id')->select();
        foreach ($b as $k => $v){
  $aa = date("Y-m-d",$v['create_time']);
            db('cmf_test_wx_jg_album')->where(['id'=>$v['id']])->update(['create_time'=>$aa]);
            
        }
        
        return 111;die;
        
        
        
        $post = request()->only([
            'page',
            'pagesize'
        ], 'post');
        
        if (! isset($post['page'])) {
            $post['page'] = 0; // 分页页数
        }
        
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10; // 分页条数
        }
        
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        
        // 公司列表
        $res['company'] = db('company')->alias('a')
            ->field('
                a.id,
                a.company_name,
                (select count(*) from applet_project where 
                company_id = a.id and status = 1 and review_status = 1 and state =1) as number')
            ->order('a.sort asc')
            ->where([
            'a.status' => 1
        ])
            ->page($page, $pagesize)
            ->select();
        // 默认项目列表
        $res['item'] = db('project')->field('id,project_name,project_model')
            ->where([
            'company_id' => $res['company'][0]['id'],
            'review_status' => 1
        ])
            ->order('progress desc')
            ->page($page, $pagesize)
            ->select();
        
        // 绑定的
        $MyBound = $this->MyBound($this->accessData['uid']);
        
        // 关注的
        $MyFollow = $this->MyFollow($this->accessData['uid']);
        
        foreach ($res['item'] as $k => $v) {
            if (in_array($v['id'], $MyBound)) {
                $res['item'][$k]['bound'] = 1;
            } else {
                $res['item'][$k]['bound'] = 0;
            }
            
            if (in_array($v['id'], $MyFollow)) {
                $res['item'][$k]['follow'] = 1;
            } else {
                $res['item'][$k]['follow'] = 0;
            }
        }
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/item_list",
     * tags={"index"},
     * summary="首页-项目切换",
     * description="首页-项目切换-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="分页参数-页码", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="pagesize", description="分页参数-页数", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="id", description="公司id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="用户ID"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="project_model", type="string", description="项目模型图片")
     * )
     * )
     */
    public function item_list()
    {
        $post = request()->only([
            'page',
            'pagesize',
            'id'
        ], 'post');
        if (! isset($post['page'])) {
            $post['page'] = 0;
        }
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        if (! isset($post['id'])) {
            err('公司id不能为空');
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        $res = db('project')->field('id,project_name,project_model')
            ->where([
            'company_id' => $post['id'],
            'review_status' => 1,
            'status' => 1,
            'state' => 1
        ])
            ->page($page, $pagesize)
            ->select();
        
        // 绑定的
        $MyBound = $this->MyBound($this->accessData['uid']);
        
        // 关注的
        $MyFollow = $this->MyFollow($this->accessData['uid']);
        
        foreach ($res as $k => $v) {
            if (in_array($v['id'], $MyBound)) {
                $res[$k]['bound'] = 1;
            } else {
                $res[$k]['bound'] = 0;
            }
            
            if (in_array($v['id'], $MyFollow)) {
                $res[$k]['follow'] = 1;
            } else {
                $res[$k]['follow'] = 0;
            }
        }
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/BrushList",
     * tags={"Brush"},
     * summary="刷刷标题",
     * description="刷刷标题 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="name", type="integer", description="同事圈"),
     * @SWG\Header(header="names", type="integer", description="要闻榜"),
     * )
     * )
     */
    public function BrushList()
    {
        $res['name'] = '同事圈';
        $res['names'] = '要闻榜';
        
        if ($res) {
            suc('', $res);
        } else {
            err('暂无数据');
        }
    }

    /**
     * @SWG\Post(
     * path="/BrushWeekly",
     * tags={"Brush"},
     * summary="刷刷周报",
     * description="刷刷周报 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="create_time", type="integer", description="累计天数"),
     * @SWG\Header(header="city_number", type="integer", description="市数量"),
     * @SWG\Header(header="county_number", type="integer", description="县数量"),
     * @SWG\Header(header="address_number", type="integer", description="安置点数量"),
     * @SWG\Header(header="plan_finish_area1", type="integer", description="计划完成面积"),
     * @SWG\Header(header="plan_output1", type="integer", description="计划产值"),
     * @SWG\Header(header="recovery_money", type="integer", description="回收产值"),
     * )
     * )
     */
    public function BrushWeekly()
    {
        $where['state'] = 1;
        $where['review_status'] = 1;
        $where['status'] = 1;

        
        $res = db('wx_jg_weekly')->find();

        $res['create_time'] = $res['day']; // 扶贫累计天数
        // 扶贫项目总计 县 数量
        $arr = db('project')->field('address,SUM(plan_finish_area1) as plan_finish_area1,SUM(plan_output1) as plan_output1,SUM(recovery_money) as recovery_money')
            ->where($where)
            ->select();

        $res['city_number'] = $res['city'];
        $res['county_number'] = $res['county']; // 扶贫项目总计县数量
        $res['address_number'] = $res['resettlement']; // 扶贫项目总计安置点数量
        $res['plan_finish_area1'] = $arr[0]['plan_finish_area1']; // 扶贫计划完成面积
        $res['plan_output1'] = $arr[0]['plan_output1']; // 扶贫计划产值
        $res['recovery_money'] = $arr[0]['recovery_money']; // 扶贫累计回收产值
        
        if ($res) {
            suc('', $res);
        } else {
            err('暂无数据');
        }
    }

    /**
     * @SWG\Post(
     * path="/BrushJournalism",
     * tags={"Brush"},
     * summary="刷刷内容",
     * description="刷刷内容 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Header(header="type", type="integer", description="类型 1-同事圈 2-要闻榜"),
     * @SWG\Header(header="page", type="integer", description="页数"),
     * @SWG\Header(header="pagesize", type="integer", description="每页条数"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="圈圈ID"),
     * @SWG\Header(header="head_img", type="integer", description="头像"),
     * @SWG\Header(header="user_name", type="integer", description="昵称"),
     * @SWG\Header(header="item_id", type="integer", description="项目ID"),
     * @SWG\Header(header="item_name", type="integer", description="项目名称"),
     * @SWG\Header(header="speed_name", type="integer", description="描述"),
     * @SWG\Header(header="give-name", type="integer", description="点赞人名"),
     * @SWG\Header(header="count", type="integer", description="点赞数"),
     * @SWG\Header(header="is", type="integer", description="是否点过赞 1-点过 0-没点"),
     * )
     * )
     */
    public function BrushJournalism()
    {
        $post = request()->only([
            'type',
            'page',
            'pagesize'
        ], 'post');
        if (empty($post['type'])) {
            $type = 1;
        } else {
            $type = $post['type'];
        }
        
        if (! isset($post['page'])) {
            $post['page'] = 0; // 分页页数
        }
        
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10; // 分页条数
        }
        
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        
        // 要闻榜
        if ($type == 2) {
            // 重要公告
            $arr['artice'] = db('wx_jg_article')->field("id,title,countet,FROM_UNIXTIME(create_time,'%Y-%m-%d') as create_time")
                ->order('create_time desc')
                ->limit(3)
                ->select();
            // 检查整改
            $arr['applics'] = db('wx_jg_application')->field("id,item_id,item_name,results,FROM_UNIXTIME(create_time,'%Y-%m-%d') as create_time")
                ->where([
                'sty' => 0
            ])
                ->order('create_time desc')
                ->limit(2)
                ->select();
            // 整改反馈
        $arr['app'] = db('wx_jg_applics')->alias('a')
                ->join('wx_jg_application b', 'a.cid = b.id')
                ->field("a.id,a.cid,a.describe,b.item_name,a.create_time")
                ->order('a.create_time desc')
                ->limit(2)
                ->select();
            foreach ($arr['app'] as $k => $v){
                $arr['app'][$k]['create_time'] = date("Y-m-d",strtotime($v['create_time']));
            }
        } else {
            // 同事圈
            $arr = db('wx_jg_coworker')->field('id,head_img,user_name,image,item_id,item_name,speed_name,create_time')
                ->page($page, $pagesize)
                ->order('create_time desc')
                ->select();
            foreach ($arr as $k => $v) {
                $arr[$k]['image'] = json_decode($v['image']);
                $arr[$k]['NewTime'] = $this->format_date($v['create_time']);
            }
            // 所以点赞人
            $str = db('wx_jg_fabulous')->field('cid')
                ->limit(200)
                ->select();
            $cid_str = "";
            foreach ($arr as $k => $v) {
                $cid_str = $cid_str . $v['id'] . ',';
            }
            $str = db('wx_jg_fabulous')->field('user_id,cid,name')
                ->where([
                'cid' => [
                    'in',
                    $cid_str
                ]
            ])
                ->limit(200)
                ->select();
            
            foreach ($arr as $k => $v) {
                foreach ($str as $val) {
                    if ($v['id'] == $val['cid'])
                        $arr[$k]['give'][] = $val;
                }
                $arr[$k]['give'] = ! isset($arr[$k]['give']) || empty($arr[$k]['give']) ? [] : $arr[$k]['give'];
                $arr[$k]['count'] = ! isset($arr[$k]['give']) || empty($arr[$k]['give']) ? 0 : count($arr[$k]['give']);
            }
            foreach ($arr as $k => $v) {
                $a = array_column($v['give'], 'user_id');
                $arr[$k]['is'] = 0;
                if (in_array($this->accessData['uid'], $a)) {
                    $arr[$k]['is'] = 1;
                }
            }
        }
        
        if ($arr) {
            suc('', $arr);
        } else {
            err('暂无数据');
        }
    }

    /**
     * @SWG\Post(
     * path="/GiveFabulous",
     * tags={"Brush"},
     * summary="同事圈点赞",
     * description="同事圈点赞 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Header(header="id", type="integer", description="同事圈ID"),
     * @SWG\Header(header="item_id", type="integer", description="项目ID"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="image", type="integer", description="图片"),
     * @SWG\Header(header="speed_name", type="integer", description="标题"),
     * @SWG\Header(header="create_time", type="integer", description="时间"),
     * @SWG\Header(header="name", type="integer", description="姓名"),
     * @SWG\Header(header="head_img", type="integer", description="头像"),
     * @SWG\Header(header="projectname", type="integer", description="项目名称")
     * )
     * )
     */
    public function GiveFabulous()
    {
        $post = request()->only([
            'id',
            'item_id'
        ], 'post');
        if (empty($post['id'])) {
            err('ID不能为空');
        }
        if (empty($post['item_id'])) {
            err('项目ID不能为空');
        }
        // 判断是否重复点赞
        $arr = db('wx_jg_fabulous')->where([
            'user_id' => $this->accessData['uid'],
            'cid' => $post['id']
        ])->find();
        
        if ($arr) {
            err('请勿重复点赞');
        }
        
        // 增加点赞
        $data['user_id'] = $this->accessData['uid'];
        // 个人信息
        $name = $this->UserName($this->accessData['uid']);
        $data['name'] = $name['name'];
        $data['cid'] = $post['id'];
        $data['create_time'] = time();
        $res = db('wx_jg_fabulous')->insert($data);
        if ($res) {
            // 获取当前点赞人
            $res = db('wx_jg_fabulous')->field('name')
                ->where([
                'cid' => $post['id']
            ])
                ->select();
            if (! $res) {
                $res = [];
            }
            // 更新活跃度
            db('project')->where([
                'id' => $post['item_id']
            ])->setInc('activity_degree', 5);
            suc($res);
        } else {
            err('点赞失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/CancelFabulous",
     * tags={"Brush"},
     * summary="同事圈取消点赞",
     * description="同事圈取消点赞 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Header(header="id", type="integer", description="圈圈ID"),
     * @SWG\Header(header="item_id", type="integer", description="项目ID"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function CancelFabulous()
    {
        $post = request()->only([
            'id',
            'item_id'
        ], 'post');
        if (empty($post['id'])) {
            err('ID不能为空');
        }
        
        if (empty($post['item_id'])) {
            err('项目ID不能为空');
        }
        // 取消点赞
        $res = db('wx_jg_fabulous')->where([
            'user_id' => $this->accessData['uid'],
            'cid' => $post['id']
        ])->delete();
        
        if ($res) {
            // 更新活跃度
            db('project')->where([
                'id' => $post['item_id']
            ])->setDec('activity_degree', 5);
            // 获取当前点赞人
            $arr = db('wx_jg_fabulous')->field('name')
                ->where([
                'cid' => $post['id']
            ])
                ->select();
            if (! $arr) {
                suc('null');
            }
            
            suc($arr);
        } else {
            err('你还未点赞');
        }
    }

    /**
     * @SWG\Post(
     * path="/liveness_Ranking",
     * tags={"Ranking"},
     * summary="活跃度排行",
     * description="活跃度排行 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="item_name", type="string", description="公司名称"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="activity_degree", type="integer", description="活跃度"),
     * @SWG\Header(header="state", type="string", description="项目状态：{0=进行中，1=滞后}"),
     * @SWG\Header(header="ranking", type="integer", description="排行名次"),
     * )
     * )
     */
    public function liveness_Ranking()
    {
        
        // 查询活跃度排行
        $liveness_Ranking['ranking'] = db('project')->where("review_status = 1 and status=1")
            ->field('id,item_name,project_name,activity_degree,project_status,plan_finish_time')
            ->order('activity_degree desc,plan_finish_time desc,id desc')
            ->limit(500)
            ->select();
        
        $RankingName = db('wx_jg_bound')->field('id,item_id,name,head_img,type')
            ->limit(500)
            ->select();
        
        foreach ($liveness_Ranking['ranking'] as $key => $val) {
            $liveness_Ranking['ranking'][$key]['name'] = "";
            $liveness_Ranking['ranking'][$key]['head_img'] = "";
            foreach ($RankingName as $k => $v) {
                if ($val['id'] == $v['item_id'] and $v['type'] == 5) {
                    $liveness_Ranking['ranking'][$key]['name'] = $v['name'];
                    $liveness_Ranking['ranking'][$key]['head_img'] = $v['head_img'];
                }
            }
        }
        
        // 绑定的
        $MyBound = $this->MyBound($this->accessData['uid']);
        
        // 关注的
        $MyFollow = $this->MyFollow($this->accessData['uid']);
        
        foreach ($liveness_Ranking['ranking'] as $k => $v) {
            $liveness_Ranking['ranking'][$k]['ranking'] = $k + 1;
            if (in_array($v['id'], $MyBound)) {
                $liveness_Ranking['ranking'][$k]['bound'] = 1;
            } else {
                $liveness_Ranking['ranking'][$k]['bound'] = 0;
            }
            
            if (in_array($v['id'], $MyFollow)) {
                $liveness_Ranking['ranking'][$k]['follow'] = 1;
            } else {
                $liveness_Ranking['ranking'][$k]['follow'] = 0;
            }
        }
        
        // 查询我自己绑定的项目
        $myitem = db('wx_jg_bound')->field('item_id')
            ->where([
            'user_id' => $this->accessData['uid']
        ])
            ->select();
        // 查询我自己关注的项目
        $myfollow = db('wx_jg_follow')->field('item_id')
            ->where([
            'user_id' => $this->accessData['uid']
        ])
            ->select();
        $myitem = array_column($myitem, 'item_id');
        $myfollow = array_column($myfollow, 'item_id');
        $myall = array_merge($myitem, $myfollow);
        $myall = array_unique($myall);
        $liveness_Ranking['my_ranking'] = [];
        foreach ($liveness_Ranking['ranking'] as $k => $v) {
            if (in_array($v['id'], $myall)) {
                $liveness_Ranking['my_ranking'][] = $v;
            }
        }
        $new = $liveness_Ranking['ranking'][0]['project_name'];
        
        // 判断数据是否为空
        if ($liveness_Ranking) {
            // 添加到排行表
            $skt = db('wx_jg_ranking')->where([
                'type' => 3
            ])->find();
            if (! $skt) {
                db('wx_jg_ranking')->insert([
                    'name' => $liveness_Ranking['ranking'][0]['project_name'],
                    'type' => 3
                ]);
            } else {
                db('wx_jg_ranking')->where([
                    'type' => 3
                ])->update([
                    'name' => $liveness_Ranking['ranking'][0]['project_name']
                ]);
            }
            suc('', $liveness_Ranking);
        } else {
            err('暂无排行数据');
        }
    }

    /**
     * @SWG\Post(
     * path="/project_Ranking",
     * tags={"Ranking"},
     * summary="项目进度排行",
     * description="项目进度排行 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=false, type="integer", default="0"),
     * @SWG\Parameter(name="pagesize", description="每页条数", in="formData", required=false, type="integer", default="30"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="item_name", type="string", description="公司名称"),
     * @SWG\Header(header="project_name", type="int", description="项目名称"),
     * @SWG\Header(header="progress", type="int", description="项目进度(%)"),
     * )
     * )
     */
    public function project_Ranking()
    {
        $aaa = db('project')->field('city_id')
            ->where([
            'state' => 1,
            'review_status' => 1,
            'status' => 1
        ])
            ->select();
        $result = array();
        
        foreach ($aaa as $val) {
            $key = $val['city_id'];
            if (! isset($result[$key])) {
                $result[$key] = $val;
            }
        }
        // 查询项目进度排行
        $project_Ranking['ranking'] = db('project')->where("review_status=1 and status=1 ")
            ->field('id,item_name,project_name,progress')
            ->order('progress desc ,id')
            ->select();
        
        $RankingName = db('wx_jg_bound')->field('id,item_id,name,head_img,type')->select();
        
        foreach ($project_Ranking['ranking'] as $key => $val) {
            $project_Ranking['ranking'][$key]['name'] = "";
            $project_Ranking['ranking'][$key]['head_img'] = "";
            foreach ($RankingName as $k => $v) {
                if ($val['id'] == $v['item_id'] and $v['type'] == 5) {
                    $project_Ranking['ranking'][$key]['name'] = $v['name'];
                    $project_Ranking['ranking'][$key]['head_img'] = $v['head_img'];
                }
            }
        }
        
        // 绑定的
        $MyBound = $this->MyBound($this->accessData['uid']);
        
        // 关注的
        $MyFollow = $this->MyFollow($this->accessData['uid']);
        
        foreach ($project_Ranking['ranking'] as $k => $v) {
            $project_Ranking['ranking'][$k]['ranking'] = $k + 1;
            if (in_array($v['id'], $MyBound)) {
                $project_Ranking['ranking'][$k]['bound'] = 1;
            } else {
                $project_Ranking['ranking'][$k]['bound'] = 0;
            }
            
            if (in_array($v['id'], $MyFollow)) {
                $project_Ranking['ranking'][$k]['follow'] = 1;
            } else {
                $project_Ranking['ranking'][$k]['follow'] = 0;
            }
        }
        
        // 查询我自己绑定的项目
        $myitem = db('wx_jg_bound')->field('item_id')
            ->where([
            'user_id' => $this->accessData['uid']
        ])
            ->select();
        // 查询我自己关注的项目
        $myfollow = db('wx_jg_follow')->field('item_id')
            ->where([
            'user_id' => $this->accessData['uid']
        ])
            ->select();
        $myitem = array_column($myitem, 'item_id');
        $myfollow = array_column($myfollow, 'item_id');
        $myall = array_merge($myitem, $myfollow);
        $myall = array_unique($myall);
        $project_Ranking['my_ranking'] = [];
        foreach ($project_Ranking['ranking'] as $k => $v) {
            if (in_array($v['id'], $myall)) {
                $project_Ranking['my_ranking'][] = $v;
            }
        }
        
        // 判断数据是否为空
        if ($project_Ranking) {
            // 添加到排行表
            $skt = db('wx_jg_ranking')->where([
                'type' => 2
            ])->find();
            if (! $skt) {
                db('wx_jg_ranking')->insert([
                    'name' => $project_Ranking['ranking'][0]['project_name'],
                    'type' => 2
                ]);
            } else {
                db('wx_jg_ranking')->where([
                    'type' => 2
                ])->update([
                    'name' => $project_Ranking['ranking'][0]['project_name']
                ]);
            }
            suc('', $project_Ranking);
        } else {
            err('暂无排行数据');
        }
    }

    /**
     * @SWG\Post(
     * path="/project_plan",
     * tags={"Project"},
     * summary="项目进度（没用）",
     * description="项目进度 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="plan", type="integer", description="项目进度(%)"),
     * @SWG\Header(header="state", type="integer", description="项目状态：{0=进行中，1=滞后}"),
     * )
     * )
     */
    public function project_Plan()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
        $project_id = input('id'); // 项目id
                                   
        // 查询项目详情
        $project_Plan = db('project')->where("id='{$project_id}'")
            ->field('id,progress,project_status')
            ->find();
        
        // 判断数据是否为空
        if ($project_Plan) {
            suc('', $project_Plan);
        } else {
            err('暂无该项目');
        }
    }

    /**
     * @SWG\Post(
     * path="/project_Details",
     * tags={"Project"},
     * summary="项目详情-项目概况",
     * description="项目详情-项目概况 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="project_model", type="string", description="项目模型图"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="progress", type="integer", description="项目进度(%)"),
     * @SWG\Header(header="project_status", type="integer", description="项目状态：{1=进行中 2=已完成 0=滞后中}"),
     * @SWG\Header(header="item_name", type="string", description="承建单位"),
     * @SWG\Header(header="plan_output_total", type="integer", description="工程造价"),
     * @SWG\Header(header="engin_time", type="integer", description="开工时间"),
     * @SWG\Header(header="total_area", type="integer", description="建设面积"),
     * @SWG\Header(header="can_count", type="integer", description="楼栋数量"),
     * @SWG\Header(header="album-image", type="integer", description="图片"),
     * @SWG\Header(header="album-speed_name", type="integer", description="描述"),
     * @SWG\Header(header="album-create_time", type="integer", description="日期"),
     *
     * )
     * )
     */
    public function project_Details()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
        $project_id = input('id'); // 项目id
                                   
        // 查询项目详情
        $project_Details = db('project')->where("id='{$project_id}'")
            ->field('id,project_model,can_count,project_name,progress,project_status,item_name,plan_output_total,engin_time,total_area')
            ->find();
        if ($project_Details) {
            // 绑定的
            $MyBound = $this->MyBound($this->accessData['uid']);
            
            // 关注的
            $MyFollow = $this->MyFollow($this->accessData['uid']);
            
            if (in_array($project_Details['id'], $MyBound)) {
                $project_Details['bound'] = 1;
            } else {
                $project_Details['bound'] = 0;
            }
            
            if (in_array($project_Details['id'], $MyFollow)) {
                $project_Details['follow'] = 1;
            } else {
                $project_Details['follow'] = 0;
            }
        }
        // 现场照片
        $project_Details['album'] = db('wx_jg_album')->field('image,speed_name,create_time')
            ->where([
            'item_id' => $project_id
        ])
            ->order('time_new desc')
            ->find();
        
        $project_Details['album']['image'] = json_decode($project_Details['album']['image']);
        
        // 判断数据是否为空
        if ($project_Details) {
            suc('', $project_Details);
        } else {
            err('暂无该项目');
        }
    }

    /**
     * @SWG\Post(
     * path="/cloud_Album",
     * tags={"Project"},
     * summary="项目详情-云相册(和我的项目-项目-公用)",
     * description="项目详情-云相册 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="type", description="类型入口 1-首页项目进来 2-我的项目进去", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=false, type="integer", default="0"),
     * @SWG\Parameter(name="pagesize", description="每页条数", in="formData", required=false, type="int", default="30"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="model-project_model", type="integer", description="模型图"),
     * @SWG\Header(header="member-head_img", type="string", description="头像"),
     * @SWG\Header(header="member-position", type="integer", description="职位名称"),
     * @SWG\Header(header="album-image", type="integer", description="云相册图片"),
     * @SWG\Header(header="album-speed_name", type="integer", description="描述"),
     * @SWG\Header(header="album-create_time", type="integer", description="时间"),
     * @SWG\Header(header="album-NewTime", type="integer", description="已过日期"),
     * @SWG\Header(header="is", type="integer", description="1-有权限 2-无权限"),
     * )
     * )
     */
    public function cloud_Album()
    {
        $post = request()->only([
            'id',
            'type',
            'page',
            'pagesize'
        ], 'post');
        
        if (empty($post['type'])) {
            err('入口类型不能为空');
        }
        
        if (! isset($post['page'])) {
            $post['page'] = 0;
        }
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        
        if ($post['type'] == 1) {
            if (empty($post['id'])) {
                err('项目ID不能为空');
            } else {
                $item_id = $post['id'];
            }
        } else {
            if (empty($post['id'])) {
                // 获取默认项目id
                $str = db('wx_jg_bound')->field('item_id')
                    ->order('item_id asc')
                    ->where([
                    'user_id' => $this->accessData['uid']
                ])
                    ->find();
                $item_id = $str['item_id'];
            } else {
                $item_id = $post['id'];
            }
            
            // 已绑定的项目列表
            $res['item'] = db('wx_jg_bound')->alias('a')
                ->join('project b', 'a.item_id = b.id', 'left')
                ->field('b.id,b.project_name')
                ->where([
                'a.user_id' => $this->accessData['uid']
            ])
                ->select();
            
            if (! $res['item']) {
                err('亲，你还没有绑定过项目');
            }
        }
        // 模型图
        $res['model'] = db('project')->field('project_model')
            ->where([
            'id' => $item_id
        ])
            ->find();
        // 项目成员
        $res['member'] = db('wx_jg_bound')->field('head_img,position')
            ->where([
            'item_id' => $item_id
        ])
            ->select();
        // 查询云相册
        $res['album'] = db('wx_jg_album')->where([
            'item_id' => $item_id
        ])
            ->field('image,speed_name,create_time,time_new')
            ->order('time_new desc')
            ->page($page, $pagesize)
            ->select();
        if (! $res['album']) {
            $res['album'] = [];
        }
        foreach ($res['album'] as $k => $v) {
            $res['album'][$k]['image'] = json_decode($v['image']);
            // 上传多长时间
            $res['album'][$k]['NewTime'] = $this->format_date($v['time_new']);
        }
        // 判断是否有权限
        $arra = db('wx_jg_bound')->where([
            'user_id' => $this->accessData['uid'],
            'item_id' => $item_id
        ])->find();
        if ($arra) {
            $res['is'] = 1;
        } else {
            $res['is'] = 0;
        }
        suc('', $res);
    }

    /**
     * @SWG\Post(
     * path="/project_Bound",
     * tags={"Bound"},
     * summary="项目成员(不用)",
     * description="项目成员 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=false, type="integer", default="0"),
     * @SWG\Parameter(name="pagesize", description="每页条数", in="formData", required=false, type="integer", default="30"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="ID"),
     * @SWG\Header(header="user_id", type="integer", description="用户id"),
     * @SWG\Header(header="item_id", type="integer", description="项目id"),
     * @SWG\Header(header="head_img", type="string", description="头像"),
     * @SWG\Header(header="name", type="string", description="姓名"),
     * @SWG\Header(header="position_id", type="integer", description="职位id"),
     * @SWG\Header(header="position", type="string", description="职位"),
     * @SWG\Header(header="type", type="integer", description="阶梯"),
     * @SWG\Header(header="sort", type="integer", description="排序"),
     * @SWG\Header(header="code", type="string", description="邀请码"),
     * @SWG\Header(header="create_time", type="integer", description="绑定时间"),
     * )
     * )
     */
    public function project_Bound()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
        $project_id = input('id'); // 项目id
                                   
        // 查询项目成员
        $project_Bound = db('wx_jg_bound')->where("item_id='{$project_id}'")
            ->order('type asc')
            ->select();
        
        // 判断数据是否为空
        if ($project_Bound) {
            suc('', $project_Bound);
        } else {
            err('暂无项目成员绑定');
        }
    }

    /**
     * @SWG\Post(
     * path="/upload_Album",
     * tags={"Project"},
     * summary="项目详情-项云相册-上传云相册",
     * description="项目详情-项云相册-上传云相册 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="image", description="图片", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="speed_name", description="施工进度描述", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="create_time", description="记录时间", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="integer", description="上传成功"),
     * )
     * )
     */
    public function upload_Album()
    {
        $post = request()->only([
            'id',
            'image',
            'speed_name',
            'create_time'
        ], 'post');
        
        // 接收数据判断区
        if (empty($post['id'])) {
            err('项目ID不能为空');
        }
        if (empty($post['image'])) {
            err('上传图片不能为空');
        }
        if (empty($post['speed_name'])) {
            err('描述不能为空');
        }
        if (empty($post['create_time'])) {
            $post['create_time'] = date("Y-m-d");
        }
        // 组合上传数据
        $data['item_id'] = $post['id']; // 项目id
        $data['image'] = $post['image']; // 图片
                                         
        // 获取图片数量
        $number = json_decode($post['image']);
        $number = count($number);
        $number = $number * 10;
        
        $data['speed_name'] = $post['speed_name']; // 施工进度描述
        $data['create_time'] = $post['create_time']; // 记录时间
        $data['user_id'] = $this->accessData['uid']; // 用户ID
        $data['time_new'] = time(); // 上传时间
                                    
        // 上传-云相册
        $upload_Album = db('wx_jg_album')->insertGetId($data);
        // 判断数据是否为空
        if ($upload_Album) {
            // 上传同事圈图片
            $data = [];
            $data['item_id'] = $post['id'];
            $data['image'] = $post['image'];
            $data['speed_name'] = $post['speed_name'];
            $data['create_time'] = time();
            $data['type'] = 1;
            $data['type_id'] = $upload_Album;
            // 个人信息
            $will = $this->UserName($this->accessData['uid']);
            $data['user_id'] = $this->accessData['uid'];
            $data['user_name'] = $will['name'];
            $data['head_img'] = $will['avatarurl'];
            // 项目名称
            $item = $this->ProjectName($post['id']);
            $data['item_name'] = $item['project_name'];
            db('wx_jg_coworker')->insert($data);
            // 更新活跃度
            db('project')->where([
                'id' => $post['id']
            ])->setInc('activity_degree', $number);
            
            // 更新连续3天和大于3天活跃度
            $time = strtotime(date('Y-m-d', time()));
            $where = [];
            $where['item_id'] = $post['id'];
            $where['image'] = [
                'neq',
                ''
            ];
            $where['time_new'] = [
                [
                    'lt',
                    $time
                ],
                [
                    'gt',
                    $time - 60 * 60 * 24 * 3
                ]
            ];
            $new_time = db('wx_jg_album')->field('time_new')
                ->order('time_new desc')
                ->where($where)
                ->select();
            if (count($new_time) == 3) {
                // 更新活跃度
                db('project')->where([
                    'id' => $post['id']
                ])->setInc('activity_degree', 50);
            } else {
                $where['time_new'] = [
                    [
                        'lt',
                        $time
                    ],
                    [
                        'gt',
                        $time - 60 * 60 * 24 * 2
                    ]
                ];
                $new_time = db('wx_jg_album')->field('time_new')
                    ->order('time_new desc')
                    ->where($where)
                    ->select();
                if (count($new_time) == 2) {
                    db('project')->where([
                        'id' => $post['id']
                    ])->setInc('activity_degree', 100);
                }
            }
            suc('上传成功');
        } else {
            err('上传失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/memor_list",
     * tags={"Project"},
     * summary="项目详情-验收认证",
     * description="项目详情-大事记-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=false, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="is_Permissions", type="string", description="权限 1有权限 0无权限"),
     * @SWG\Header(header="res-sort", type="string", description="排序值"),
     * @SWG\Header(header="res-event_name", type="string", description="大事记名称"),
     * @SWG\Header(header="res-event_time", type="string", description="大事记日期"),
     * @SWG\Header(header="res-event_img", type="string", description="大事记图片"),
     * @SWG\Header(header="res-state", type="string", description="审核状态 0 未审核 1 已审核"),
     * )
     * )
     */
    public function memor_list()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (empty($post['id'])) {
            err('项目id不能为空');
        }
        // 判断是否有权限
        $arra = db('wx_jg_bound')->where([
            'user_id' => $this->accessData['uid'],
            'item_id' => $post['id']
        ])->find();
        if ($arra) {
            $is_Permissions['is_Permissions'] = 1;
        } else {
            $is_Permissions['is_Permissions'] = 0;
        }
        
        $is_Permissions['res'] = db('wx_jg_event')->field('sort,event_name,event_time,event_img,state')
            ->where([
            'item_id' => $post['id']
        ])
            ->order('sort asc')
            ->select();
        
        suc($is_Permissions);
    }

    /**
     * @SWG\Post(
     * path="/memor_add",
     * tags={"Project"},
     * summary="项目详情-验收认证-提交",
     * description="项目详情-大事记-提交-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="froms", description="json格式[{"event_name":"\u5927\u4e8b\u8bb0\u540d\u79f0","event_time":"\u65e5\u671f","event_img":"\u56fe\u7247","sort":"\u6392\u5e8f\u503c"}]", in="formData", required=false, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function memor_add()
    {
        $post = request()->only([
            'id',
            'froms'
        ], 'post');
        if (empty($post['id'])) {
            err('项目id不能为空');
        }
        
        $froms = json_decode($post['froms'], true);
        
        foreach ($froms as $k => $v) {
            // 查询原有数据
            $arr = db('wx_jg_event')->where([
                'sort' => $v['sort'],
                'item_id' => $post['id']
            ])->find();
            $type_id = ""; // 圈圈分类ID
            if (! $arr) {
                $data['user_id'] = $this->accessData['uid'];
                $data['item_id'] = $post['id'];
                $data['event_name'] = $v['event_name'];
                $data['event_time'] = $v['event_time'];
                $data['event_img'] = $v['event_img'];
                $data['sort'] = $v['sort'];
                $data['create_time'] = time();
                $res = db('wx_jg_event')->insertGetId($data);
                $type_id = $res;
                if ($res) {
                    db('project')->where([
                        'id' => $post['id']
                    ])->setInc('activity_degree', 10);
                }
            } else {
                $data['event_name'] = $v['event_name'];
                $data['event_time'] = $v['event_time'];
                $data['event_img'] = $v['event_img'];
                $res = db('wx_jg_event')->where([
                    'sort' => $v['sort'],
                    'item_id' => $post['id']
                ])->update($data);
                $type_id = $post['id'];
                if ($res) {
                    db('wx_jg_event')->where([
                        'sort' => $v['sort']
                    ])->update([
                        'state' => 0
                    ]);
                    db('project')->where([
                        'id' => $post['id']
                    ])->setInc('activity_degree', 10);
                }
            }
            
            // if ($res){
            // $data = [];
            // $data['item_id'] = $post['id'];
            // $data['image'] = $v['event_img'];
            // $data['speed_name'] = $v['event_name'];
            // $data['create_time'] = date("Y-m-d");
            // $data['type'] = 3;
            // $data['type_id'] = $type_id;
            // //个人信息
            // $will = $this->UserName($this->accessData['uid']);
            // $data['user_id'] = $this->accessData['uid'];
            // $data['user_name'] = $will['name'];
            // $data['head_img'] = $will['avatarurl'];
            // //项目名称
            // $item = $this->ProjectName($post['id']);
            // $data['item_name'] = $item['project_name'];
            // db('wx_jg_coworker')->insert($data);
            // }
        }
        
        suc('提交成功');
    }

    /**
     * @SWG\Post(
     * path="/Lagging",
     * tags={"Project"},
     * summary="项目详情-验收认证-更改项目状态-是否滞后",
     * description="项目详情-验收认证-更改项目状态-是否滞后-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="status", description="状态2-滞后 1-正常", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="countent", description="原因", in="formData", required=false, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Lagging()
    {
        $post = request()->only([
            'id',
            'countent',
            'status'
        ], 'post');
        if (empty($post['id'])) {
            err('项目id不能为空');
        }
        
        if (empty($post['status'])) {
            err('状态不能为空');
        } elseif ($post['status'] == 2) {
            // 更新项目状态、和原因
            $res = db('project')->where([
                'id' => $post['id']
            ])->update([
                'project_status' => 0,
                'delay_reason' => $post['countent']
            ]);
            if ($arr) {
                suc('修改成功');
            } else {
                err('修改失败');
            }
        } else {
            // 更新项目状态
            $res = db('project')->where([
                'id' => $post['id']
            ])->update([
                'project_status' => 1,
                'delay_reason' => ""
            ]);
        }
        if ($res) {
            suc('修改成功');
        } else {
            err('修改失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/Project",
     * tags={"Project"},
     * summary="项目详情-项目",
     * description="项目详情-项目 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目id"),
     * @SWG\Header(header="item_id", type="integer", description="项目指定id"),
     * @SWG\Header(header="project_name", type="string", description="项目名"),
     * @SWG\Header(header="province_id", type="string", description="省"),
     * @SWG\Header(header="city_id", type="string", description="市"),
     * @SWG\Header(header="address", type="string", description="街道地址"),
     * @SWG\Header(header="construction_unit", type="string", description="施工单位"),
     * @SWG\Header(header="contract_unit", type="string", description="建设单位"),
     * @SWG\Header(header="design_unit", type="string", description="设计单位"),
     * @SWG\Header(header="supervisor_unit", type="string", description="监理单位"),
     * @SWG\Header(header="structure", type="string", description="结构形式"),
     * @SWG\Header(header="total_area", type="string", description="总建筑面积"),
     * @SWG\Header(header="plan_output_total", type="string", description="工程造价"),
     * @SWG\Header(header="finished_area", type="string", description="主体完工面积"),
     * @SWG\Header(header="renovation_area", type="string", description="装修完工面积"),
     * @SWG\Header(header="finished_output", type="string", description="累计完成产值"),
     * @SWG\Header(header="plan_finish_time", type="string", description="计划竣工时间"),
     * @SWG\Header(header="is", type="string", description="判断是否有权限 1-有 0-无"),
     * )
     * )
     */
    public function Project()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
        $project_id = input('id'); // 项目id
                                   
        // 查询项目详情
        $field = [
            'id,project_name,province_id,city_id,address,construction_unit,contract_unit, 
        design_unit,supervisor_unit,structure,total_area,plan_output_total,finished_area,renovation_area,finished_output,
        recovery_money, plan_finish_time,can_count'
        ];
        $project_Details = db('project')->field($field)
            ->where("id='{$project_id}'")
            ->find();
        
        // 判断是否有权限
        $arra = db('wx_jg_bound')->where([
            'user_id' => $this->accessData['uid'],
            'item_id' => $project_id
        ])->find();
        if ($arra) {
            $project_Details['is'] = 1;
        } else {
            $project_Details['is'] = 0;
        }
        
        // 判断数据是否为空
        if ($project_Details) {
            // 分解结构
            if (empty($project_Details['structure'])) {
                $project_Details['structure1'] = '';
                $project_Details['structure2'] = '';
            } else {
                $project_Details['structure'] = explode('+', $project_Details['structure']);
                if (! empty($project_Details['structure'])) {
                    if (! empty($project_Details['structure'][0])) {
                        $project_Details['structure1'] = $project_Details['structure'][0];
                    }
                    if (! empty($project_Details['structure'][1])) {
                        $project_Details['structure2'] = $project_Details['structure'][1];
                    }
                }
            }
            
            // 查询省市区
            $project_Details['province_id'] = db('area')->where("id='{$project_Details['province_id']}'")->value('name');
            $project_Details['city_id'] = db('area')->where("id='{$project_Details['city_id']}'")->value('name');
            
            suc('', $project_Details);
        } else {
            err('暂无该项目');
        }
    }

    /**
     * @SWG\Post(
     * path="/project_Submit",
     * tags={"Project"},
     * summary="项目详情-项目提交",
     * description="项目详情-项目提交 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="project_name", type="string", description="项目名", in="formData", required=true),
     * @SWG\Parameter(name="province_id", type="string", description="省", in="formData", required=true),
     * @SWG\Parameter(name="city_id", type="string", description="市", in="formData", required=true),
     * @SWG\Parameter(name="address", type="string", description="街道地址", in="formData", required=true),
     * @SWG\Parameter(name="construction_unit", type="string", description="施工单位", in="formData", required=true),
     * @SWG\Parameter(name="contract_unit", type="string", description="建设单位", in="formData", required=true),
     * @SWG\Parameter(name="design_unit", type="string", description="设计单位", in="formData", required=true),
     * @SWG\Parameter(name="supervisor_unit", type="string", description="监理单位", in="formData", required=true),
     * @SWG\Parameter(name="structure1", type="string", description="结构形式1", in="formData", required=true),
     * @SWG\Parameter(name="structure2", type="string", description="结构形式2", in="formData", required=true),
     * @SWG\Parameter(name="total_area", type="string", description="总建筑面积", in="formData", required=true),
     * @SWG\Parameter(name="plan_output_total", type="string", description="工程造价", in="formData", required=true),
     * @SWG\Parameter(name="finished_area", type="string", description="主体完工面积", in="formData", required=true),
     * @SWG\Parameter(name="renovation_area", type="string", description="装修完工面积", in="formData", required=true),
     * @SWG\Parameter(name="finished_output", type="string", description="累计完成产值", in="formData", required=true),
     * @SWG\Parameter(name="recovery_money", type="string", description="回收工程款", in="formData", required=true),
     * @SWG\Parameter(name="plan_finish_time", type="string", description="计划竣工时间", in="formData", required=true),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="integer", description="提交成功"),
     * )
     * )
     */
    public function project_Submit()
    {
        // 接收数据
        $project_id = input('id'); // 项目id
        $user_id = $this->accessData['uid']; // 用户id
        $info = request()->only([
            'project_name',
            'province_id',
            'city_id',
            'address',
            'construction_unit',
            'supervisor_unit',
            'contract_unit',
            'design_unit',
            'structure1',
            'structure2',
            'total_area',
            'plan_output_total',
            'finished_area',
            'renovation_area',
            'finished_output',
            'recovery_money',
            'plan_finish_time'
        ], 'post');
        
        // 查询项目详情
        $project_Details = db('project')->where("id='{$project_id}'")->find();
        // 判断数据是否为空
        if (! $project_Details) {
            err('暂无该项目');
        }
        
        $data = [];
        // #组合更新数据
        // 活跃度
        $vitality = 0;
        $count_number = 0;
        // 项目名
        if (empty($project_Details['project_name'])) {
            if (! empty($info['project_name'])) {
                $vitality += 10;
                $data['project_name'] = $info['project_name'];
            }
        }
        // 省
        if (! empty($info['province_id'])) {
            // 获取id
            $info['province_id'] = db('area')->where("name='{$info['province_id']}'")->value('id');
            $vitality += 10;
            $data['province_id'] = $info['province_id'];
        }
        
        // 市
        if (! empty($info['city_id'])) {
            // 获取id
            $info['city_id'] = db('area')->where("name='{$info['city_id']}'")->value('id');
            $vitality += 10;
            $data['city_id'] = $info['city_id'];
        }
        // 街道地址
        if (! empty($info['address'])) {
            $vitality += 10;
            $data['address'] = $info['address'];
        }
        
        // 施工单位
        if (! empty($info['construction_unit'])) {
            $vitality += 10;
            $data['construction_unit'] = $info['construction_unit'];
            $data['item_name'] = $info['construction_unit'];
            $company_id = db('company')->field('id')
                ->where([
                'company_name' => $info['construction_unit']
            ])
                ->find();
            $data['company_id'] = $company_id['id'];
        }
        
        // 建设单位
        if (! empty($info['contract_unit'])) {
            $vitality += 10;
            $data['contract_unit'] = $info['contract_unit'];
        }
        
        // 设计单位
        if (! empty($info['design_unit'])) {
            $vitality += 10;
            $data['design_unit'] = $info['design_unit'];
        }
        
        // 监理单位
        if (! empty($info['supervisor_unit'])) {
            $vitality += 10;
            $data['supervisor_unit'] = $info['supervisor_unit'];
        }
        
        // 结构形式
        if (! empty($info['structure1']) && ! empty($info['structure2'])) {
            $vitality += 10;
            $data['structure'] = $info['structure1'] . '+' . $info['structure2'];
        }
        
        // 总建筑面积
        if (! empty($info['total_area'])) {
            $vitality += 10;
            $data['total_area'] = $info['total_area'];
        }
        // 工程造价
        if (! empty($info['plan_output_total'])) {
            $vitality += 10;
            $data['plan_output_total'] = $info['plan_output_total'];
            if ($info['plan_output_total'] < $info['recovery_money']) {
                err('回收工程款不能大于工程造价');
            }
        }
        // 主体完工面积
        if (! empty($info['finished_area'])) {
            if ($info['finished_area'] != $project_Details['finished_area']) {
                $count_number += 10;
                $vitality += 10;
                $data['finished_area'] = $info['finished_area'];
            }
        }
        // 装修完工面积
        if (! empty($info['renovation_area'])) {
            if ($info['renovation_area'] != $project_Details['renovation_area']) {
                $count_number += 10;
                $vitality += 10;
                $data['renovation_area'] = $info['renovation_area'];
            }
        }
        // 累计完成产值
        if (! empty($info['finished_output'])) {
            if ($info['finished_output'] != $project_Details['finished_output']) {
                $count_number += 10;
                $vitality += 10;
                $data['finished_output'] = $info['finished_output'];
            }
        }
        // 回收工程款
        if (! empty($info['recovery_money'])) {
            $data['recovery_money'] = $info['recovery_money'];
        }
        
        // 计划竣工时间
        if (! empty($info['plan_finish_time'])) {
            $vitality += 10;
            $data['plan_finish_time'] = $info['plan_finish_time'];
        }
        
        // 更新项目资料
        $project_Submit = db('project')->where("id='{$project_id}'")->update($data);
        
        // 判断数据是否为空
        if ($project_Submit) {
            // 更新活跃度
            db('project')->where("id='{$project_id}'")->setInc('activity_degree', $count_number);
            
            // 更新回款率
            $Payment = ceil($data['recovery_money'] / $project_Details['plan_output_total'] * 100);
            db('project')->where("id='{$project_id}'")->setField('pay_rank', $Payment);
            
            suc('提交成功');
        } else {
            err('提交失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/frame_Structure",
     * tags={"Project"},
     * summary="项目详情-项目提交-框架结构",
     * description="项目详情-项目提交-框架结构 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="框架结构id"),
     * @SWG\Header(header="structure_name", type="string", description="框架结构名"),
     * @SWG\Header(header="create_time", type="integer", description="创建时间"),
     * )
     * )
     */
    public function frame_Structure()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
                                              
        // 查询框架结构
        $frame_Structure = db('wx_jg_structure')->field('id,structure_name,create_time')->select();
        
        // 判断数据是否为空
        if ($frame_Structure) {
            suc('', $frame_Structure);
        } else {
            err('暂无项目成员绑定');
        }
    }

    /**
     * @SWG\Post(
     * path="/building_Build",
     * tags={"Project"},
     * summary="项目详情-建造记录-页面",
     * description="项目详情-建造记录-页面 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="ban_number", description="楼栋号", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="project-id", type="integer", description="项目ID"),
     * @SWG\Header(header="project-can_count", type="integer", description="楼栋总数"),
     * @SWG\Header(header="project-project_status", type="string", description="项目状态 1 正常 2已完成 3 滞后中"),
     * @SWG\Header(header="record-sort", type="string", description="排序"),
     * @SWG\Header(header="record-speed_describe", type="integer", description="记录描述"),
     * @SWG\Header(header="record-speed", type="string", description="进度"),
     * @SWG\Header(header="record_image", type="integer", description="图片"),
     * )
     * )
     */
    public function building_Build()
    {
        // 接收数据
        $project_id = input('item_id'); // 项目id
        $ban_number = input('ban_number'); // 楼栋号
                                           
        // 查询楼栋总数和项目状态
        $res['project'] = db('project')->where("id='{$project_id}'")
            ->field('id,can_count,project_status,delay_reason as countent')
            ->find();
        // if ($res['project']['project_status'] == 0){
        // $str = db('wx_jg_lagging')->field('countent')->where(['item_id'=>$project_id])->order('create_time desc')->find();
        // $res['project']['countent'] = $str['countent'];
        // }
        // 查询建造记录
        $res['record'] = db('wx_jg_record')->where([
            'item_id' => $project_id,
            'ban_number' => $ban_number
        ])
            ->field('id,sort,speed_describe,speed,image')
            ->select();
        // 当前楼栋总进度
        $number = 0;
        foreach ($res['record'] as $k => $v) {
            if (! empty($v['image'])) {
                $res['record'][$k]['image'] = json_decode($v['image']);
            }
            $number = $number + $v['speed'];
        }
        
        $res['number'] = $number;
        suc('', $res);
    }

    /**
     * @SWG\Post(
     * path="/open_building_Build",
     * tags={"Project"},
     * summary="项目详情-建造记录-页面-提交",
     * description="项目详情-建造记录-页面-提交 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="ban_number", description="楼栋号", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="froms", description="json和大事记格式一样[{描述},{进度},{图片}]", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="number_count", description="上传图片数量", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="打开成功"),
     * )
     * )
     */
    public function open_building_Build()
    {
        $post = request()->only([
            'item_id',
            'ban_number',
            'froms',
            'number_count'
        ], 'post');
        
        if (empty($post['item_id'])) {
            err('项目ID不能为空');
        }
        
        if (empty($post['ban_number'])) {
            err('楼栋号不能为空');
        }
        
        // 当前时间日期
        $new_day = date("Y-m-d");
        $days = "";
        // 有进度就更改项目状态-已开工
        $msts = db('project')->field('progress,number,update_day')
            ->where([
            'id' => $post['item_id']
        ])
            ->find();
        
        if (! empty($msts['update_day'])) {
            $days = strtotime($new_day) - strtotime($msts['update_day']);
            if ($days > 86400) {
                db('project')->where([
                    'id' => $post['item_id']
                ])->update([
                    'number' => 0
                ]);
            }
            
            if ($msts['progress'] > 0) {
                // 修改成竣工状态
                db('project')->where([
                    'id' => $post['item_id']
                ])->update([
                    'start_work' => 1
                ]);
            }
        }
        
        // 获取上传图片的数量来增加活跃度 一张图片+10点
        $sum_number = 0;
        if (! empty($post['number_count'])) {
            $sum_number = $post['number_count'] * 10;
        }
        // 前端传数据json
        $froms = json_decode($post['froms'], true);
        
        if (! is_array($froms)) {
            err('参数错误');
        }
        
        foreach ($froms as $k => $v) {
            $image = json_encode($v['image']);
            $where = [];
            $where['item_id'] = $post['item_id'];
            $where['ban_number'] = $post['ban_number'];
            $where['sort'] = $v['sort'];
            $mstr = db('wx_jg_record')->where($where)->find();
            $type_id = ""; // 圈圈类型ID
            
            if ($mstr) {
                $res = db('wx_jg_record')->where([
                    'sort' => $v['sort'],
                    'item_id' => $post['item_id'],
                    'ban_number' => $post['ban_number']
                ])->update([
                    'image' => $image
                ]);
                
                $type_id = $mstr['id'];
            } else {
                $data = [];
                $data['user_id'] = $this->accessData['uid'];
                $data['item_id'] = $post['item_id'];
                $data['speed_describe'] = $v['speed_describe'];
                $data['speed'] = $v['speed'];
                $data['ban_number'] = $post['ban_number'];
                $data['image'] = $image;
                $data['create_time'] = time();
                $data['sort'] = $v['sort'];
                $res = db('wx_jg_record')->insertGetId($data);
                $type_id = $res;
            }
            
            if ($res and $image != "[]") {
                $data = [];
                $data['item_id'] = $post['item_id'];
                $data['image'] = $image;
                $data['speed_name'] = $v['speed_describe'];
                $data['create_time'] = time();
                $data['type'] = 2;
                $data['type_id'] = $type_id;
                // 个人信息
                $will = $this->UserName($this->accessData['uid']);
                $data['user_id'] = $this->accessData['uid'];
                $data['user_name'] = $will['name'];
                $data['head_img'] = $will['avatarurl'];
                // 项目名称
                $item = $this->ProjectName($post['item_id']);
                $data['item_name'] = $item['project_name'];
                db('wx_jg_coworker')->insert($data);
            }
        }
        if ($res) {
            
            // 查询项目所有进度
            $all = db('wx_jg_record')->where([
                'item_id' => $post['item_id']
            ])
                ->field('ban_number')
                ->group('ban_number')
                ->select();
            $sum = [];
            foreach ($all as $key => $value) {
                $where = [];
                $where['item_id'] = $post['item_id'];
                $where['ban_number'] = $value['ban_number'];
                $all_value = db('wx_jg_record')->where($where)
                    ->field('SUM(speed) as speed')
                    ->order('speed desc')
                    ->find();
                $sum[] = $all_value['speed'];
            }
            $sum = array_sum($sum);
            
            // 获取楼栋总数
            $cont = db('project')->field('can_count,activity_degree')
                ->where([
                'id' => $post['item_id']
            ])
                ->find();
            $project_all_progress = $sum / $cont['can_count'];
            $project_all_progress_number = $cont['activity_degree'] + ($sum * 10);
            // 更新项目进度
            if ($project_all_progress > 0) {
                db('project')->where([
                    'id' => $post['item_id']
                ])->update([
                    'progress' => $project_all_progress
                ]);
            }
            // 更新项目活跃度
            if ($sum_number > 0) {
                db('project')->where([
                    'id' => $post['item_id']
                ])->setInc('activity_degree', $sum_number);
            }
            
            db('project')->where([
                'id' => $post['item_id']
            ])->update([
                'update_day' => date("Y-m-d")
            ]);
            
            if ($days == 86400) {
                $data = [];
                $data['update_day'] = date("Y-m-d");
                $data['number'] = $msts['number'] + 1;
                db('project')->where([
                    'id' => $post['item_id']
                ])->update($data);
            }
            
            // 再次判断提交次数
            $max = db('project')->field('number')
                ->where([
                'id' => $post['item_id']
            ])
                ->find();
            if ($max['number'] == 3) {
                db('project')->where([
                    'id' => $post['item_id']
                ])->setInc('activity_degree', 100);
            } elseif ($max['number'] > 3) {
                db('project')->where([
                    'id' => $post['item_id']
                ])->setInc('activity_degree', 50);
            }
            
            suc('提交成功');
        } else {
            err('提交失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/uploadingImg_Build",
     * tags={"Project"},
     * summary="楼栋建造记录上传图片",
     * description="楼栋建造记录上传图片 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="楼栋建造记录id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="image", description="图片（json）", in="formData", required=false, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="打开成功"),
     * )
     * )
     */
    public function uploadingImg_Build()
    {
        // 接收数据
        $Build_id = input('id'); // 楼栋建造记录id
        $user_id = $this->accessData['uid']; // 用户id
        $info['image'] = input('image'); // 多图
                                         
        // 判断是否有上传权限
        $is_Permissions = $this->is_Permissions($user_id, $project_id);
        $is_Permissions = json_decode($is_Permissions, true);
        if ($is_Permissions['is_Permissions'] == 0) {
            err('无权限');
        }
        
        // 接收数据判断区
        if (empty($project_id)) {
            err('item_id NOT NULL');
        }
        if (empty($info['image'])) {
            err('image NOT NULL');
        }
        
        // 更新楼栋建造记录图片
        $uploadingImg_Build = db('wx_jg_record')->where("id='{$Build_id}'")->update([
            'image' => $info['image']
        ]);
        
        // 判断数据是否为空
        if ($uploadingImg_Build) {
            suc('上传成功');
        } else {
            err('上传失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/framedData_Submit",
     * tags={"Project"},
     * summary="楼栋建造记录提交",
     * description="楼栋建造记录提交 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="formData", description="值", in="formData", required=true, type="array"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="打开成功"),
     * )
     * )
     */
    public function framedData_Submit()
    {
        // 接收数据
        $user_id = $this->accessData['uid']; // 用户id
        $project_id = input('item_id'); // 项目id
        $info['formData'] = input('formData/a'); // 值
                                                 // 接收数据判断区
        if (empty($project_id)) {
            err('item_id NOT NULL');
        }
        if (! is_array($info)) {
            err('formData Is not an array');
        }
        
        // 判断是否有上传权限
        $is_Permissions = $this->is_Permissions($user_id, $project_id);
        $is_Permissions = json_decode($is_Permissions, true);
        if ($is_Permissions['is_Permissions'] == 0) {
            err('无权限');
        }
        
        foreach ($info['formData'] as $value) {
            if (empty($value['id'])) {
                // 组合添加数组
                $data = [];
                $data['user_id'] = $user_id;
                $data['item_id'] = $project_id;
                $data['speed_describe'] = $value['speed_describe'];
                $data['speed'] = $value['speed'];
                $data['ban_number'] = $value['ban_number'];
                $data['create_time'] = time();
                
                if (empty($value['ban_number'])) {
                    err('ban_number NOT NULL');
                }
                if (empty($value['speed_describe'])) {
                    err('speed_describe NOT NULL');
                }
                if (empty($value['image'])) {
                    err('image NOT NULL');
                }
                if (db('wx_jg_record')->where("item_id='{$project_id}' and ban_number='{$value['ban_number']}' and speed='{$value['speed']}'")->find()) {
                    err('progress already exist');
                }
                $value['image'] = json_encode($value['image']);
                if (empty($value['image'])) {
                    err('image BE NULL');
                }
                
                $data['image'] = $value['image'];
                
                // 添加楼栋建造记录
                $open_building_Build = db('wx_jg_record')->insertGetId($data);
                
                // 判断数据是否为空
                if ($open_building_Build) {}
            } else {
                // 接收数据判断区
                if (empty($value['id'])) {
                    err('id NOT NULL');
                }
                if (empty($value['image'])) {
                    err('image NOT NULL');
                }
                $value['image'] = json_encode($value['image']);
                if (empty($value['image'])) {
                    err('image BE NULL');
                }
                
                // 更新楼栋建造记录图片
                $uploadingImg_Build = db('wx_jg_record')->where("id='{$value['id']}'")->update([
                    'image' => $value['image']
                ]);
            }
        }
        
        // 项目栋数
        $House_number = db('project')->where("id='{$project_id}'")->value('can_count');
        
        // 查询项目所有进度
        $all = db('wx_jg_record')->where("item_id='{$project_id}'")
            ->field('ban_number')
            ->group('ban_number')
            ->select();
        foreach ($all as $key => $value) {
            $all_value[$key] = db('wx_jg_record')->where("item_id='{$project_id}' and ban_number='{$value['ban_number']}'")
                ->order('speed desc')
                ->value('speed');
        }
        $project_all_progress = ceil((array_sum($all_value) / ($House_number * 100)) * 100);
        // 更新项目进度
        db('project')->where("id='{$project_id}'")->update([
            'progress' => $project_all_progress
        ]);
        // 更新项目状态
        if ($project_all_progress == 100) {
            db('project')->where("id='{$project_id}'")->update([
                'project_status' => 2
            ]);
        }
        
        suc('提交成功');
    }

    /**
     * @SWG\Post(
     * path="/lag_Switch",
     * tags={"Project"},
     * summary="滞后开关(不用)",
     * description="滞后开关 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="integer")
     * @SWG\Parameter(name="is_lag", description="是否滞后（1=否，0=是）", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="打开成功"),
     * )
     * )
     */
    public function lag_Switch()
    {
        // 接收数据
        $project_id = input('item_id'); // 项目id
        $user_id = $this->accessData['uid']; // 用户id
        $info['ban_number'] = input('ban_number'); // 楼栋号
        $is_lag = input('is_lag') ? input('is_lag') : 0; // 是否滞后（1=否，0=是）
                                                         
        // 接收数据判断区
        if (empty($project_id)) {
            err('item_id NOT NULL');
        }
        
        // 更新项目进度
        $lag_Switch = db('project')->where("id='{$project_id}'")->update([
            'project_status' => $is_lag
        ]);
        
        // 判断数据是否为空
        if ($lag_Switch) {
            suc('打开成功');
        } else {
            err('打开失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/project_progress",
     * tags={"Project"},
     * summary="项目进度",
     * description="项目进度 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="progress", type="integer", description="项目进度(%)"),
     * @SWG\Header(header="state", type="integer", description="项目状态：{0=进行中，1=滞后}"),
     * )
     * )
     */
    public function project_progress()
    {
        // 接收数据
        $admin_id = $this->accessData['aid']; // 客户id
        $info['ban_number'] = input('ban_number'); // 楼栋号
        $project_id = input('id'); // 项目id
                                   
        // 查询项目详情
        $project_progress = db('project')->where("id='{$project_id}'")
            ->field('id,progress,project_status')
            ->find();
        
        $project_progress['progress'] = db('wx_jg_record')->where("item_id='{$project_id}' and ban_number='{$info['ban_number']}'")
            ->order('speed desc')
            ->value('speed');
        if (empty($project_progress['progress'])) {
            $project_progress['progress'] = 0;
        }
        // 判断数据是否为空
        if ($project_progress) {
            suc('', $project_progress);
        } else {
            err('暂无该项目');
        }
    }

    /**
     * @SWG\Post(
     * path="/invite_Bindingframework",
     * tags={"Bound"},
     * summary="邀请绑定职位架构（不用）",
     * description="邀请绑定职位架构 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="职位架构id"),
     * @SWG\Header(header="position_name", type="string", description="职位名称"),
     * @SWG\Header(header="type", type="integer", description="阶梯"),
     * @SWG\Header(header="sort", type="integer", description="排序"),
     * @SWG\Header(header="create_time", type="integer", description="创建时间"),
     * )
     * )
     */
    public function invite_Bindingframework()
    {
        // 查询阶梯
        $invite_type = db('wx_jg_framework')->field('type')
            ->group('type')
            ->order('type asc')
            ->select();
        
        // 查询阶梯下的职位
        foreach ($invite_type as $key => $value) {
            $k = $value['type'];
            $invite_Bindingframework[] = db('wx_jg_framework')->where("type='$k'")
                ->order('sort asc')
                ->select();
        }
        
        suc('', $invite_Bindingframework);
    }

    /**
     * @SWG\Post(
     * path="/invite_BindingList",
     * tags={"Bound"},
     * summary="邀请绑定页面",
     * description="邀请绑定页面 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="id"),
     * @SWG\Header(header="position_name", type="integer", description="职位名称"),
     * @SWG\Header(header="type", type="integer", description="排序值"),
     * )
     * )
     */
    public function invite_BindingList()
    {
        $res['name'] = "";
        $res['tel'] = "";
        if (! empty($this->accessData['uid'])) {
            $user = db('wx_jg_personal')->where([
                'user_id' => $this->accessData['uid']
            ])->find();
            $res['name'] = $user['name'];
            $res['tel'] = $user['tel'];
        }
        $res['framework'] = db('wx_jg_framework')->field('id,position_name,type')
            ->order('type asc')
            ->select();
        suc('', $res);
    }

    /**
     * @SWG\Post(
     * path="/invite_Binding",
     * tags={"Bound"},
     * summary="邀请绑定",
     * description="邀请绑定 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="code", description="邀请码", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="name", description="姓名", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="tel", description="手机号码", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="排序值", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="绑定成功"),
     * )
     * )
     */
    public function invite_Binding()
    {
        // 接收数据
        $project_id = input('item_id'); // 项目id
        $user_id = $this->accessData['uid']; // 用户id
        $info['code'] = input('code'); // 邀请码
        $info['name'] = input('name'); // 姓名
        $info['tel'] = input('tel'); // 手机号码
        $info['type'] = input('type'); // 序列值
        $info['create_time'] = $this->timeStamp; // 创建时间
                                                 
        // 接收数据判断区
        if (empty($project_id)) {
            err('item_id NOT NULL');
        }
        if (empty($info['code'])) {
            err('code NOT NULL');
        }
        if (empty($info['name'])) {
            err('name NOT NULL');
        }
        if (empty($info['tel'])) {
            err('tel NOT NULL');
        }
        if (empty($info['type'])) {
            err('type NOT NULL');
        }
        // 判断邀请码是否正确
        $code = db('project')->field('id')
            ->where([
            'code' => $info['code'],
            'review_status' => 1,
            'id' => $project_id
        ])
            ->find();
        if (! $code) {
            err('邀请码不存在或该项目还未审核');
        }
        
        // 判断绑定人重复绑定该项目职位
        if (db('wx_jg_bound')->where("item_id='{$project_id}' and user_id='{$user_id}'")->find()) {
            err('已绑定过该项目');
        }
        
        // 获取用户信息
        $user = db('applet_user')->where("id='{$user_id}'")
            ->field('id,avatarurl,nickname,gender')
            ->find();
        
        // 组合添加数据
        $info['item_id'] = input('item_id'); // 项目id
        $info['user_id'] = $user_id; // 用户id
        $info['head_img'] = $user['avatarurl']; // 头像
                                                
        // 获取职位信息
        $str = db('wx_jg_framework')->field('position_name')
            ->where([
            'type' => $info['type']
        ])
            ->find();
        $info['position'] = $str['position_name'];
        // 添加绑定成员
        $invite_Binding = db('wx_jg_bound')->insertGetId($info);
        
        // 判断数据是否为空
        if ($invite_Binding) {
            db('project')->where([
                'id' => $project_id
            ])->setInc('activity_degree', 10); // 关注一位用户活跃度+10
            suc('绑定成功');
        } else {
            err('绑定失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/invite_Bindingframeworkmember",
     * tags={"Bound"},
     * summary="职位架构成员",
     * description="职位架构成员 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="head_img", type="integer", description="ID"),
     * @SWG\Header(header="user_id", type="integer", description="用户id"),
     * @SWG\Header(header="item_id", type="integer", description="项目id"),
     *
     * )
     * )
     */
    public function invite_Bindingframeworkmember()
    {
        // 接收数据
        $project_id = input('id'); // 项目id
        if (empty($project_id)) {
            err('项目id不能为空');
        }
        
        $res = db('wx_jg_bound')->field('head_img,name,type')
            ->where([
            'item_id' => $project_id
        ])
            ->order('type')
            ->select();
        
        $data = [];
        
        foreach ($res as $key => $value) {
            $data[$value['type']]['key'] = $value['type'];
            $data[$value['type']]['value'][] = $value;
        }
        $data = array_values($data);
        suc('', $data);
    }

    /**
     * @SWG\Post(
     * path="/personage_Centre",
     * tags={"User"},
     * summary="个人中心",
     * description="个人中心 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="user_id", type="string", description="用户id"),
     * @SWG\Header(header="name", type="string", description="姓名"),
     * @SWG\Header(header="tel", type="string", description="手机号码"),
     * @SWG\Header(header="company", type="string", description="公司"),
     * @SWG\Header(header="department", type="string", description="部门"),
     * @SWG\Header(header="duties", type="string", description="职务"),
     * @SWG\Header(header="item_duties", type="string", description="项目职务"),
     * )
     */
    public function personage_Centre()
    {
        // 接收数据
        $user_id = $this->accessData['uid']; // 用户id
                                             
        // 查询个人中心数据
        $personage_Centre = db('wx_jg_personal')->field('id,name,tel,company,department,duties,item_duties')
            ->where("user_id='{$user_id}'")
            ->find();
        
        // 判断数据是否为空
        if ($personage_Centre) {
            suc('', $personage_Centre);
        } else {
            err('用户暂无信息');
        }
    }

    /**
     * @SWG\Post(
     * path="/personage_Centre_edit",
     * tags={"User"},
     * summary="个人中心编辑",
     * description="个人中心编辑 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="name", type="string", description="姓名", in="formData", required=false),
     * @SWG\Parameter(name="tel", type="string", description="手机号码", in="formData", required=false),
     * @SWG\Parameter(name="company", type="string", description="公司", in="formData", required=false),
     * @SWG\Parameter(name="department", type="string", description="部门", in="formData", required=false),
     * @SWG\Parameter(name="duties", type="string", description="职务", in="formData", required=false),
     * @SWG\Parameter(name="item_duties", type="string", description="项目职务", in="formData", required=false),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="编辑成功"),
     * )
     * )
     */
    public function personage_Centre_edit()
    {
        // 接收数据
        $user_id = $this->accessData['uid']; // 用户id
        $info['name'] = input('name'); // 姓名
        $info['tel'] = input('tel'); // 手机号码
        $info['company'] = input('company'); // 公司
        $info['department'] = input('department'); // 部门
        $info['duties'] = input('duties'); // 职务
        $info['item_duties'] = input('item_duties'); // 项目职务
                                                     
        // 判断是否存在
        $personage_Centre = db('wx_jg_personal')->where("user_id='{$user_id}'")->find();
        if ($personage_Centre) {
            $personage_Centre_edit = db('wx_jg_personal')->where("user_id='{$user_id}'")->update($info);
        } else {
            $info['user_id'] = $user_id;
            $info['create_time'] = $this->timeStamp;
            $personage_Centre_edit = db('wx_jg_personal')->insertGetId($info);
        }
        
        // 判断数据是否为空
        if ($personage_Centre_edit) {
            suc('编辑成功');
        } else {
            err('编辑失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/edit_DXF",
     * tags={"User"},
     * summary="编辑项目模型图",
     * description="编辑项目模型图 - 夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="project_model", description="项目模型图", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="info", type="string", description="编辑成功"),
     * )
     * )
     */
    public function edit_DXF()
    {
        // 接收数据
        $project_id = input('id'); // 项目id
        $user_id = $this->accessData['uid']; // 用户id
        $info['project_model'] = input('project_model'); // 项目模型图
                                                         
        // 判断是否有上传权限
        $is_Permissions = $this->is_Permissions($user_id, $project_id);
        $is_Permissions = json_decode($is_Permissions, true);
        if ($is_Permissions['is_Permissions'] == 0) {
            err('无权限');
        }
        
        // 接收数据判断区
        if (empty($project_id)) {
            err('id NOT NULL');
        }
        if (empty($info['project_model'])) {
            err('project_model NOT NULL');
        }
        
        // 编辑项目模型图
        $edit_DXF = db('project')->where("id = '{$project_id}'")->update([
            'project_model' => $info['project_model']
        ]);
        
        // 判断数据是否为空
        if ($edit_DXF) {
            suc('编辑成功');
        } else {
            err('编辑失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/bulletin",
     * tags={"User"},
     * summary="周报",
     * description="周报 - 吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="undertake_project", type="string", description="承接项目"),
     * @SWG\Header(header="overall_floorage", type="string", description="总建筑面积"),
     * @SWG\Header(header="number_of_contracts_signed", type="string", description="已签合同数量"),
     * @SWG\Header(header="commenced_project", type="string", description="已开工项目"),
     * @SWG\Header(header="normal_project", type="string", description="正常项目"),
     * @SWG\Header(header="lagging_behind_the_project", type="string", description="滞后项目"),
     * @SWG\Header(header="subject_area", type="string", description="主体完工面积"),
     * @SWG\Header(header="subject_area", type="string", description="装修完工面积"),
     * @SWG\Header(header="total_project_cost", type="string", description="承建项目总造价"),
     * @SWG\Header(header="accumulated_output_value", type="string", description="已开工项目累计完成产值"),
     * @SWG\Header(header="recovery_of_project_funds", type="string", description="累计回收工程款"),
     * )
     * )
     */
    public function bulletin()
    {
        $project = db('project')->where('review_status = 1 and state = 1 and status = 1')
            ->field('
                sum(total_area) as total_area,
                sum(finished_area) as finished_area,
                sum(renovation_area) as renovation_area,
                sum(plan_output_total) as plan_output_total,
                sum(finished_output) as finished_output,
                sum(recovery_money) as recovery_money,
                sum(pact_status) as pact_status,
                count(*) as project_count
            ')
            ->find();
        
        // 承接项目
        $bulletin['Management']['undertake_project'] = $project['project_count'];
        // 总建筑面积
        $bulletin['Management']['overall_floorage'] = $project['total_area'];
        // 已签合同数量
        $bulletin['Management']['number_of_contracts_signed'] = db('project')->where('review_status=1 and pact_status=1 and state = 1 and status = 1')->count();
        ;
        
        // 已开工项目
        $bulletin['Progress']['commenced_project'] = db('project')->where('start_work=1 and state = 1 and status = 1 and review_status = 1 and pact_status=1')->count();
        // 正常项目
        $bulletin['Progress']['normal_project'] = db('project')->where('review_status=1 and state = 1 and status = 1')
            ->where([
            'project_status' => [
                'in',
                '1,2'
            ]
        ])
            ->count();
        // 滞后项目
        $bulletin['Progress']['lagging_behind_the_project'] = db('project')->where('review_status=1 and state = 1 and status = 1')
            ->where("project_status=0")
            ->count();
        // 已创建模型
        $bulletin['Progress']['model'] = db('project')->where('review_status=1 and state = 1 and status = 1')
            ->where([
            'project_model' => [
                'neq',
                ''
            ]
        ])
            ->count();
        // 主体完工面积
        $bulletin['Progress']['subject_area'] = $project['finished_area'];
        // 装修完工面积
        $bulletin['Progress']['finished_area'] = $project['renovation_area'];
        
        // 承建项目总造价
        $compay = db('company')->where([
            'state' => 1,
            'status' => 1
        ])->sum('amount_all');
        $bulletin['Economics']['total_project_cost'] = $compay;
        // 已开工项目累计完成产值
        $bulletin['Economics']['accumulated_output_value'] = $project['finished_output'];
        $recovery_money = db('company')->where([
            'state' => 1,
            'status' => 1
        ])->sum('payment');
        // 累计回收工程款
        $bulletin['Economics']['recovery_of_project_funds'] = $recovery_money;
        
        // 标题
        $bulletin['title']['title'] = '易地扶贫搬迁项目运行简报';
        // 时间
        $bulletin['title']['date'] = date('Y-m-d');
        
        // 排行指标
        // 项目活跃度冠军
        $bulletin['Ranking']['active'] = db('wx_jg_ranking')->field('name')
            ->where([
            'type' => 3
        ])
            ->find();
        // 项目进度冠军
        $bulletin['Ranking']['progress'] = db('wx_jg_ranking')->field('name')
            ->where([
            'type' => 2
        ])
            ->find();
        // 公司进度
        $bulletin['Ranking']['company'] = db('wx_jg_ranking')->field('name')
            ->where([
            'type' => 1
        ])
            ->find();
        // 回款率
        $bulletin['Ranking']['payment'] = db('wx_jg_ranking')->field('name')
            ->where([
            'type' => 4
        ])
            ->find();
        suc($bulletin);
    }

    /**
     * @SWG\Post(
     * path="/my_item",
     * tags={"Project"},
     * summary="我的项目",
     * description="我的项目-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型(3,我的 1,已绑定的 2,已关注的)", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="page", description="分页参数-页码", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="pagesize", description="分页参数-页数", in="formData", required=false, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="用户ID"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="item_name", type="string", description="公司名称"),
     * @SWG\Header(header="progress", type="string", description="项目进度"),
     * @SWG\Header(header="state", type="string", description="项目状态0-进行中 1-滞后中"),
     * @SWG\Header(header="project_model", type="string", description="项目模型图片")
     * )
     * )
     */
    public function my_item()
    {
        $post = request()->only([
            'page',
            'pagesize',
            'type'
        ], 'post');
        
        if (empty($post['page'])) {
            $post['page'] = 0;
        }
        if (empty($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        
        $pagesize = $post['pagesize'];
        
        if ($post['page'] == 0 or $post['page'] == 1) {
            $page = 0;
        } else {
            $page = $post['page'] * $pagesize;
        }
        
        if (empty($post['type']) || $post['type'] == 3) {
            // 我的项目
            $res = db('project')->where([
                'review_status' => 1,
                'add_uid' => $this->accessData['uid']
            ])
                ->field('id,project_model,item_name,project_name,progress,project_status')
                ->order('create_time desc')
                ->limit($page, $pagesize)
                ->select();
        } elseif ($post['type'] == 1) {
            // 已绑定过项目
            $arr = db('wx_jg_bound')->field('item_id')
                ->where([
                'user_id' => $this->accessData['uid']
            ])
                ->limit($page, $pagesize)
                ->select();
            if (! $arr) {
                err('你还未绑定过');
            }
            foreach ($arr as $k => $v) {
                $res[$k] = db('project')->field('id,project_model,item_name,project_name,progress,project_status')
                    ->where([
                    'id' => $v['item_id'],
                    'review_status' => 1
                ])
                    ->find();
                if ($res[$k] == null) {
                    unset($res[$k]);
                }
            }
        } elseif ($post['type'] == 2) {
            // 已关注过的项目
            $arr = db('wx_jg_follow')->field('item_id')
                ->where([
                'user_id' => $this->accessData['uid']
            ])
                ->limit($page, $pagesize)
                ->select();
            
            if (! $arr) {
                err('你还未关注过');
            }
            foreach ($arr as $k => $v) {
                $res[$k] = db('project')->field('id,project_model,item_name,project_name,progress,project_status')
                    ->where([
                    'id' => $v['item_id'],
                    'review_status' => 1
                ])
                    ->find();
                $res[$k]['is'] = "1";
            }
        }
        
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/my_item_list",
     * tags={"Project"},
     * summary="我的项目-下拉项目列表",
     * description="我的项目-下拉项目列表-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string")
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="项目ID"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * )
     * )
     */
    public function my_item_list()
    {
        // 查询我绑定的项目
        $where['a.state'] = 1;
        $where['a.review_status'] = 1;
        $where['a.status'] = 1;
        $where['b.user_id'] = $this->accessData['uid'];
        $res = db('project')->alias('a')
            ->join('wx_jg_bound b', 'a.id = item_id')
            ->field('a.id,a.project_name')
            ->where($where)
            ->select();
        if (! $res) {
            err('没有数据');
        }
        suc('', $res);
    }

    /**
     * @SWG\Post(
     * path="/new_item",
     * tags={"Project"},
     * summary="新建项目",
     * description="新建项目-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="head_img", description="头像", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="公司id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="project_model", description="项目模型图", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_name", description="公司名称", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="plan_output_total", description="工程造价", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="total_area", description="工程面积", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="engin_time", description="开工时间", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function new_item()
    {
        $post = request()->only([
            'id',
            'project_dxf',
            'item_name',
            'engin_money',
            'engin_area',
            'can_count'
        ], 'post');
        if (! isset($post['project_dxf'])) {
            err('模型图不能为空');
        }
        if (! isset($post['id'])) {
            err('公司id不能为空');
        }
        if (! isset($post['item_name'])) {
            err('公司名称不能为空');
        }
        if (! isset($post['engin_money'])) {
            err('工程造价不能为空');
        }
        if (! isset($post['engin_area'])) {
            err('面积不能为空');
        }
        if (! empty($post['can_count'])) {
            $data['can_count'] = $post['can_count'];
        }
        // 随机生成
        $rand = rand(500, 999999);
        // 判断生产项目编号是否存在
        $last_num = true;
        while ($last_num) {
            $arr = db('project')->where([
                'project_no' => $rand
            ])->find();
            if (empty($arr)) {
                $last_num = false;
            }
        }
        
        $data['review_status'] = 0;
        $data['code'] = 88888;
        $data['project_no'] = $rand;
        $data['add_uid'] = $this->accessData['uid'];
        $data['company_id'] = $post['id'];
        $data['item_name'] = $post['item_name'];
        $data['construction_unit'] = $post['item_name'];
        $data['project_model'] = IMG_ADMIN . $post['project_dxf'];
        $data['plan_output_total'] = $post['engin_money'];
        $data['total_area'] = $post['engin_area'];
        $data['create_time'] = time();
        $res = db('project')->insert($data);
        if ($res < 1) {
            err('新建失败');
        } else {
            suc('新建成功');
        }
    }

    /**
     * @SWG\Post(
     * path="/bound_add",
     * tags={"Project"},
     * summary="增加关注",
     * description="增加关注-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function bound_add()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (! isset($post['id'])) {
            err('项目id不能为空');
        }
        // 判断是否关注过
        $arr = db('wx_jg_follow')->where([
            'user_id' => $this->accessData['uid'],
            'item_id' => $post['id']
        ])->find();
        if ($arr) {
            err('你已经关注了');
        } else {
            $data['user_id'] = $this->accessData['uid'];
            $data['item_id'] = $post['id'];
            $data['create_time'] = time();
            $res = db('wx_jg_follow')->insert($data);
        }
        if ($res < 1) {
            err('关注失败');
        } else {
            db('project')->where([
                'id' => $post['id']
            ])->setInc('activity_degree', 10); // 关注一位用户活跃度+10
            suc('关注成功');
        }
    }

    /**
     * @SWG\Post(
     * path="/bound_del",
     * tags={"Project"},
     * summary="取消关注",
     * description="取消关注-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function bound_del()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (! isset($post['id'])) {
            err('项目id不能为空');
        }
        $res = db('wx_jg_follow')->where([
            'item_id' => $post['id'],
            'user_id' => $this->accessData['uid']
        ])->delete();
        if ($res < 1) {
            err('取消关注失败');
        }
        suc('取消关注成功');
    }

    /**
     * @SWG\Post(
     * path="/binding_del",
     * tags={"Project"},
     * summary="取消绑定",
     * description="取消绑定-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function binding_del()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (! isset($post['id'])) {
            err('项目id不能为空');
        }
        $res = db('wx_jg_bound')->where([
            'item_id' => $post['id'],
            'user_id' => $this->accessData['uid']
        ])->delete();
        if ($res < 1) {
            err('取消绑定失败');
        }
        suc('取消绑定成功');
    }

    /**
     * @SWG\Post(
     * path="/company_rank",
     * tags={"Ranking"},
     * summary="排行榜-公司进度",
     * description="排行榜-公司进度-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="count", type="string", description="公司进度"),
     * @SWG\Header(header="id", type="string", description="公司id"),
     * @SWG\Header(header="name", type="string", description="公司名字")
     * )
     * )
     */
    public function company_rank()
    {
        $post = request()->only([
            'page',
            'pagesize'
        ], 'post');
        if (empty($post['page'])) {
            $post['page'] = 0;
        }
        if (empty($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        $where['a.review_status'] = 1;
        $where['b.state'] = 1;
        $where['a.state'] = 1;
        $where['a.status'] = 1;
        $where['b.status'] = 1;
        $where['a.project_status'] = [
            'in',
            [
                1,
                2
            ]
        ];
        
        // 获取公司正常项目
        $res = db('project')->alias('a')
            ->join('company b', 'a.company_id = b.id')
            ->field("count(*) as counts,sum(progress) as progress,a.id,b.company_name")
            ->where($where)
            ->group("a.company_id")
            ->order("counts desc")
            ->page($page, $pagesize)
            ->select();
        
        foreach ($res as $k => $v) {
            $res[$k]['count'] = floor($v['progress'] / $v['counts']);
        }
        // dump($res);die;
        // 获取公司所有项目
        // $all = db('project')->alias('a')
        // ->join('company b', 'a.company_id = b.id')
        // ->field("count(*) as counts,a.id")
        // ->where($where)
        // ->where("company_id = 7")
        // ->group("a.company_id")
        // ->select();
        
        // foreach ($res as $k => $v) {
        // foreach ($all as $key => $val) {
        // if ($v['id'] == $val['id']) {
        // $res[$k]['count'] = floor($v['count'] / $val['counts'] * 100);
        // }
        // }
        // }
        $sort = array_column($res, 'count');
        array_multisort($sort, SORT_DESC, $res);
        
        if (! $res) {
            err('没有数据');
        } else {
            // 添加到排行表
            $skt = db('wx_jg_ranking')->where([
                'type' => 1
            ])->find();
            if (! $skt) {
                db('wx_jg_ranking')->insert([
                    'name' => $res[0]['company_name'],
                    'type' => 1
                ]);
            } else {
                db('wx_jg_ranking')->where([
                    'type' => 1
                ])->update([
                    'name' => $res[0]['company_name']
                ]);
            }
            
            suc($res);
        }
    }

    /**
     * @SWG\Post(
     * path="/pay_rank",
     * tags={"Ranking"},
     * summary="排行榜-公司回款率",
     * description="排行榜-公司回款率-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="count", type="string", description="公司回款率"),
     * @SWG\Header(header="id", type="string", description="公司id"),
     * @SWG\Header(header="name", type="string", description="公司名字"),
     * @SWG\Header(header="recovery_money", type="string", description="公司总回款"),
     * @SWG\Header(header="plan_output_total", type="string", description="公司总造价"),
     * )
     * )
     */
    public function pay_rank()
    {
        $post = request()->only([
            'page',
            'pagesize'
        ], 'post');
        if (! isset($post['page'])) {
            $post['page'] = 0;
        }
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        $res = db('company')->field("(payment/amount_all) as count,id,company_name,payment as recovery_money,amount_all as plan_output_total")
            ->where([
            'state' => 1,
            'status' => 1
        ])
            ->order("(payment/amount_all) desc")
            ->page($page, $pagesize)
            ->select();
        foreach ($res as $k => $v) {
            $res[$k]['count'] = floor($v['count'] * 100);
        }
        if (! $res) {
            err('没有数据');
        } else {
            // 添加到排行表
            $skt = db('wx_jg_ranking')->where([
                'type' => 4
            ])->find();
            if (! $skt) {
                db('wx_jg_ranking')->insert([
                    'name' => $res[0]['company_name'],
                    'type' => 4
                ]);
            } else {
                db('wx_jg_ranking')->where([
                    'type' => 4
                ])->update([
                    'name' => $res[0]['company_name']
                ]);
            }
            suc($res);
        }
    }

    /**
     * @SWG\Post(
     * path="/item_bind",
     * tags={"Project"},
     * summary="绑定项目",
     * description="绑定项目-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="code", description="邀请码", in="formData", required=false, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function item_bind()
    {
        $post = request()->only([
            'code',
            'id',
            'from_page'
        ], 'post');
        if (empty($post['code'])) {
            err('请填写邀请码');
        }
        if (empty($post['id'])) {
            err('参数错误');
        }
        if (empty($post['id']) and ! isset($post['from_page'])) {
            $res = db('project')->field('id')
                ->where([
                'code' => $post['code'],
                'review_status' => 1
            ])
                ->find();
            
            if (! $res) {
                err('邀请码不存在或该项目还未审核');
            } else {
                // 获取个人信息
                $name = db('applet_user')->field('avatarurl,nickname,mobile')
                    ->where([
                    'id' => $this->accessData['uid']
                ])
                    ->find();
                $data = [];
                $data['user_id'] = $this->accessData['uid'];
                $data['item_id'] = $res['id'];
                $data['position'] = "普通员工";
                $data['code'] = $post['code'];
                $data['head_img'] = $name['avatarurl'];
                $data['name'] = $name['nickname'];
                $data['tel'] = $name['mobile'];
                $data['create_time'] = time();
                $arr = db('wx_jg_bound')->insert($data);
                if ($arr) {
                    suc('绑定成功');
                }
            }
        } elseif (! empty($post['id']) and ! empty($post['code'])) {
            $res = db('project')->where([
                'id' => $post['id'],
                'code' => $post['code'],
                'review_status' => 1
            ])->find();
            if (! $res) {
                err('邀请码不存在或该项目还未审核');
            } else {
                // 判断是否绑定过
                $str = db('wx_jg_bound')->where([
                    'user_id' => $this->accessData['uid'],
                    'item_id' => $post['id'],
                    'code' => $post['code']
                ])->find();
                if ($str) {
                    err('请勿重新绑定');
                } else {
                    // 获取个人信息
                    $name = db('applet_user')->field('avatarurl,nickname,mobile')
                        ->where([
                        'id' => $this->accessData['uid']
                    ])
                        ->find();
                    $data = [];
                    $data['user_id'] = $this->accessData['uid'];
                    $data['item_id'] = $post['id'];
                    $data['position'] = "普通员工";
                    $data['code'] = $post['code'];
                    $data['head_img'] = $name['avatarurl'];
                    $data['name'] = $name['nickname'];
                    $data['tel'] = $name['mobile'];
                    $data['create_time'] = time();
                    $arr = db('wx_jg_bound')->insert($data);
                    if ($arr) {
                        suc('绑定成功');
                    }
                }
            }
        } elseif (! empty($post['id'])) {
            $res = db('project')->field('id,code')
                ->where([
                'id' => $post['id'],
                'code' => $post['code'],
                'review_status' => 1
            ])
                ->find();
            if (! $res) {
                err('邀请码不存在或该项目还未审核');
            } else {
                // 获取个人信息
                $name = db('applet_user')->field('avatarurl,nickname,mobile')
                    ->where([
                    'id' => $this->accessData['uid']
                ])
                    ->find();
                $data = [];
                $data['user_id'] = $this->accessData['uid'];
                $data['item_id'] = $post['id'];
                $data['position'] = "普通员工";
                $data['code'] = $post['code'];
                $data['head_img'] = $name['avatarurl'];
                $data['name'] = $name['nickname'];
                $data['tel'] = $name['mobile'];
                $data['create_time'] = time();
                $arr = db('wx_jg_bound')->insert($data);
                if ($arr) {
                    suc('绑定成功');
                }
            }
        } elseif ($post['from_page'] == 'projectBound') {
            $res = db('project')->field('id')
                ->where([
                'code' => $post['code'],
                'review_status' => 1
            ])
                ->find();
            if (! $res) {
                err('邀请码不存在或该项目还未审核');
            } else {
                // 获取个人信息
                $name = db('applet_user')->field('avatarurl,nickname,mobile')
                    ->where([
                    'id' => $this->accessData['uid']
                ])
                    ->find();
                $data = [];
                $data['user_id'] = $this->accessData['uid'];
                $data['item_id'] = $res['id'];
                $data['position'] = "普通员工";
                $data['code'] = $post['code'];
                $data['head_img'] = $name['avatarurl'];
                $data['name'] = $name['nickname'];
                $data['tel'] = $name['mobile'];
                $data['create_time'] = time();
                $arr = db('wx_jg_bound')->insert($data);
                if ($arr) {
                    suc('绑定成功');
                }
            }
        }
    }

    /**
     * @SWG\Post(
     * path="/search",
     * tags={"index"},
     * summary="首页-搜索(没用)",
     * description="首页-搜索-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="search", description="搜索内容", in="formData", required=false, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="project_model", type="string", description="项目模型图"),
     * @SWG\Header(header="id", type="string", description="项目id"),
     * @SWG\Header(header="item_name", type="string", description="公司名字"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="progress", type="string", description="项目进度"),
     * @SWG\Header(header="state", type="string", description="项目状态 0 进行中 1 滞后"),
     * @SWG\Header(header="str", type="string", description="关注状态 1 已关注 0 未关注"),
     * )
     * )
     */
    public function search()
    {
        $post = request()->only([
            'search'
        ], 'post');
        if (! empty($post['search'])) {
            $where['project_name'] = [
                'like',
                '%' . $post['search'] . '%'
            ];
            $where['review_status'] = 1;
            $where['status'] = 1;
        } else {
            $where['review_status'] = 1;
            $where['status'] = 1;
        }
        $res = db('project')->field('id,project_model,item_name,project_name,progress,project_status')
            ->where($where)
            ->select();
        // 是否关注过
        foreach ($res as $k => $v) {
            $arr = db('wx_jg_follow')->where([
                'item_id' => $v['id'],
                'user_id' => $this->accessData['uid']
            ])->find();
            if ($arr) {
                $res[$k]['str'] = 1;
            } else {
                $res[$k]['str'] = 0;
            }
        }
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/screen_list",
     * tags={"index"},
     * summary="首页-搜索-筛选-页面",
     * description="首页搜索-筛选-页面-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="name", type="string", description="公司名称"),
     * )
     * )
     */
    public function screen_list()
    {
        // 公司
        $res = db('company')->where([
            'status' => 1
        ])
            ->order('sort asc')
            ->field('company_name')
            ->select();
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/screen",
     * tags={"index"},
     * summary="首页-搜索-筛选",
     * description="首页搜索-筛选-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="province_id", description="省", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="city_id", description="市", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="county_id", description="县", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="item_name", description="公司名称", in="formData", required=false, type="string"),
     * 
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="project_model", type="string", description="项目模型图"),
     * @SWG\Header(header="id", type="string", description="项目id"),
     * @SWG\Header(header="item_name", type="string", description="公司名字"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * @SWG\Header(header="progress", type="string", description="项目进度"),
     * @SWG\Header(header="state", type="string", description="项目状态 0 进行中 1 滞后"),
     * @SWG\Header(header="str", type="string", description="关注状态 1 已关注 0 未关注"),
     * )
     * )
     */
    public function screen()
    {
        $post = request()->only([
            'content',
            'province',
            'city',
            'county',
            'item_name',
            'page',
            'pagesize'
        ], 'post');
        
        if (!empty($post['content'])){
            $where['project_name'] = [
                'like',
                '%' . $post['content'] . '%'
            ];
        }
        
        if (! empty($post['province'])) {
            // 获取id
            $post['province_id'] = db('area')->where("name='{$post['province']}'")->value('id');
            $where['province_id'] = $post['province_id'];
        }
        if (! empty($post['city'])) {
            // 获取id
            $post['city_id'] = db('area')->where("name='{$post['city']}'")->value('id');
            $where['city_id'] = $post['city_id'];
        }

        if (! empty($post['item_name'])) {
            $where['item_name'] = $post['item_name'];
        }
        if (! isset($post['page'])) {
            $post['page'] = 0;
        }
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        $where['review_status'] = 1;
        $res = db('project')->field('id,project_model,item_name,project_name,progress,project_status')
            ->where($where)
            ->page($page, $pagesize)
            ->select();
        
        // 绑定的
        $MyBound = $this->MyBound($this->accessData['uid']);
        
        // 关注的
        $MyFollow = $this->MyFollow($this->accessData['uid']);
        
        foreach ($res as $k => $v) {
            if (in_array($v['id'], $MyBound)) {
                $res[$k]['bound'] = 1;
            } else {
                $res[$k]['bound'] = 0;
            }
            
            if (in_array($v['id'], $MyFollow)) {
                $res[$k]['follow'] = 1;
            } else {
                $res[$k]['follow'] = 0;
            }
        }
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/applic_list",
     * tags={"Center"},
     * summary="应用-封面图(不用)",
     * description="项目详情-应用列表-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="project_model", type="string", description="项目模型图"),
     * @SWG\Header(header="img", type="string", description="图片"),
     * )
     * )
     */
    public function applic_list()
    {
        $res = db('wx_jg_banner')->field('img')
            ->where([
            'type' => 1
        ])
            ->select();
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/applic_add",
     * tags={"Center"},
     * summary="个人中心-应用-是否有权限",
     * description="个人中心-应用-是否有权限-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型1-质检 2财务", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function applic_add()
    {
        $post = request()->only([
            'type'
        ], 'post');
        if (empty($post['type'])) {
            err('类型不能为空');
        }
        $where['user_id'] = $this->accessData['uid'];
        
        if ($post['type'] == 1) {
            $where['type'] = 1;
        } else {
            $where['type'] = 2;
        }
        
        $res = db('wx_jg_binding')->where($where)->find();
        
        if (! $res) {
            err('暂无权限');
        } else {
            suc('已有权限');
        }
    }

    /**
     * @SWG\Post(
     * path="/applic_inspect",
     * tags={"Center"},
     * summary="个人中心-应用-绑定",
     * description="个人中心-应用-绑定-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="code", description="绑定码", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="绑定码类型 1-检查 2-财务", in="formData", required=true, type="string"),
     *
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function applic_inspect()
    {
        $post = request()->only([
            'type',
            'code'
        ], 'post');
        if (empty($post['code'])) {
            err('绑定码不能为空');
        }
        if (empty($post['type'])) {
            err('绑定码类型不能为空');
        }
        
        // 判断当前绑定码是否使用
        if ($post['type'] == 2) {
            $code = db('wx_jg_binding')->where([
                'type' => 2,
                'code' => $post['code']
            ])->find();
            if ($code) {
                err('此公司财务已绑定');
            }
        }
        // 获取个人信息
        $arr = $this->UserName($this->accessData['uid']);
        
        $data['user_id'] = $this->accessData['uid'];
        $data['haed_img'] = $arr['avatarurl'];
        $data['name'] = $arr['name'];
        $data['code'] = $post['code'];
        
        // 判断绑定码是否一致
        if ($post['type'] == 1) {
            $data['type'] = 1;
            $res = db('wx_jg_code')->where([
                'type' => 1,
                'code' => $post['code']
            ])->find();
            
            if (! $res) {
                err('绑定码不正确');
            } else {
                db('wx_jg_binding')->insert($data);
                suc('验证通过');
            }
        } else {
            $data['type'] = 2;
            // 获取公司财务绑定
            $code = db('company')->field('code')
                ->where([
                'state' => 1
            ])
                ->select();
            
            $codeall = 0;
            foreach ($code as $k => $v) {
                if ($post['code'] === $v['code']) {
                    db('wx_jg_binding')->insert($data);
                    $codeall = 1;
                }
            }
            
            if ($codeall == 1) {
                suc('验证通过');
            } else {
                err('绑定码不正确');
            }
        }
    }

    /**
     * @SWG\Post(
     * path="/applic_inspect_add",
     * tags={"Center"},
     * summary="个人中心-发起检查-提交",
     * description="个人中心-发起检查-提交-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目id", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="nature", description="检查性质", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="date", description="日期", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="examiner", description="检查人", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="results", description="检查结果", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="image", description="图片", in="formData", required=false, type="string"),
     *
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据"
     * )
     * )
     */
    public function applic_inspect_add()
    {
        $post = request()->only([
            'id',
            'nature',
            'date',
            'results',
            'image'
        ], 'post');
        if (empty($post['id'])) {
            err('项目id不能为空');
        }
        if (empty($post['nature'])) {
            err('检查性质不能为空');
        }
        if (empty($poat['date'])) {
            $data['date'] = date("Y-m-d");
        } else {
            $data['date'] = $post['date'];
        }
        if (empty($post['results'])) {
            $data['results'] = "这个人很懒，什么都没留下..";
        } else {
            $data['results'] = $post['results'];
        }
        if (! empty($post['image'])) {
            $img = json_encode($post['image']);
            $data['image'] = $img;
        }
        $data['user_id'] = $this->accessData['uid'];
        $data['nature'] = $post['nature'];
        $data['item_id'] = $post['id'];
        $data['create_time'] = time();
        $data['sty'] = 0;
        // 获取项目名称
        $str = db('project')->field('project_name')
            ->where([
            'id' => $post['id']
        ])
            ->find();
        $data['item_name'] = $str['project_name'];
        $res = db('wx_jg_application')->insertGetId($data);
        
        if ($res < 1) {
            err('提交失败');
        } else {
            // 上传同事圈图片
            $data = [];
            $data['item_id'] = $post['id'];
            $data['image'] = $img;
            $data['speed_name'] = $post['results'];
            $data['create_time'] = time();
            $data['type'] = 4;
            $data['type_id'] = $res;
            // 个人信息
            $will = $this->UserName($this->accessData['uid']);
            $data['user_id'] = $this->accessData['uid'];
            $data['user_name'] = $will['name'];
            $data['head_img'] = $will['avatarurl'];
            // 项目名称
            $item = $this->ProjectName($post['id']);
            $data['item_name'] = $item['project_name'];
            db('wx_jg_coworker')->insert($data);
            // 更新活跃度
            db('project')->where([
                'id' => $post['id']
            ])->setInc('activity_degree', 10); // 关注一位用户活跃度+10
            suc('提交成功');
        }
    }

    /**
     * @SWG\Post(
     * path="/applic_lists",
     * tags={"Center"},
     * summary="个人中心-检查-整改反馈",
     * description="个人中心-检查-整改反馈-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="项目ID", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型1-未整改 2-已整改", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="分页参数-页码", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="pagesize", description="分页参数-页数", in="formData", required=false, type="integer"),
     *
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="string", description="列表id"),
     * @SWG\Header(header="results", type="string", description="问题描述"),
     * @SWG\Header(header="date", type="string", description="日期"),
     * @SWG\Header(header="project_name", type="string", description="项目名称"),
     * )
     * )
     */
    public function applic_lists()
    {
        $post = request()->only([
            'id',
            'type',
            'page',
            'pagesize'
        ], 'post');
        
        if (! empty($post['id'])) {
            $where['a.item_id'] = $post['id'];
        } else {
            $where = [];
        }
        
        if (empty($post['type'])) {
            $type = 1;
        } else {
            $type = $post['type'];
        }
        
        if (! isset($post['page'])) {
            $post['page'] = 0;
        }
        if (! isset($post['pagesize'])) {
            $post['pagesize'] = 10;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        
        if ($type == 1) {
            // 未整改
            $where['sty'] = 0;
            $res = db('wx_jg_application')->alias('a')
                ->join('project b', 'a.item_id = b.id', 'left')
                ->field('a.id,a.item_id,a.results,a.date,b.project_name')
                ->where($where)
                ->order('a.date desc')
                ->page($page, $pagesize)
                ->select();
        } else {
            // 已整改
            $res = db('wx_jg_applics')->alias('a')
                ->join('project b', 'a.item_id = b.id', 'left')
                ->field('a.id,a.cid,a.describe as results,a.create_time,b.project_name')
                ->where($where)
                ->order('a.create_time desc')
                ->page($page, $pagesize)
                ->select();
            
            foreach ($res as $k => $v) {
                $res[$k]['create_time'] = date("Y-m-d", time($v['create_time']));
            }
        }
        if (! $res) {
            err('没有数据');
        } else {
            suc($res);
        }
    }

    /**
     * @SWG\Post(
     * path="/applic_rect",
     * tags={"Center"},
     * summary="个人中心-整改反馈-未整改详情",
     * description="个人中心-整改反馈-未整改详情-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="检查列表id", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="string", description="检查列表id"),
     * @SWG\Header(header="item_id", type="string", description="项目ID"),
     * @SWG\Header(header="results", type="string", description="描述标题"),
     * @SWG\Header(header="create_time", type="string", description="日期时间"),
     * @SWG\Header(header="image", type="string", description="图片"),
     * @SWG\Header(header="is_Permissions", type="string", description="权限 1有 0无"),
     * )
     * )
     */
    public function applic_rect()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (empty($post['id'])) {
            err('列表id不能为空');
        }
        
        $arr = db('wx_jg_application')->field('id,item_id,results,create_time,image')
            ->where([
            'id' => $post['id'],
            'sty' => 0
        ])
            ->find();
        
        $arr['create_time'] = date("Y-m-d", $arr['create_time']);
        $arr['image'] = json_decode($arr['image']);
        
        // 判断是否有权限
        $is_Permissions = $this->is_Permissions($this->accessData['uid'], $arr['item_id']);
        $is_Permissions = json_decode($is_Permissions);
        $arr['is_Permissions'] = $is_Permissions->is_Permissions;
        
        if (! $arr) {
            err('没有数据');
        }
        suc($arr);
    }

    /**
     * @SWG\Post(
     * path="/applic_rect_submit",
     * tags={"Center"},
     * summary="个人中心-整改反馈-未整改提交",
     * description="个人中心-整改反馈-未整改提交-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="检查列表id", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="item_id", description="项目id", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="describe", description="整改描述", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="img", description="图片", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="is_Permissions", type="string", description="0-无权限 1-有权限"),
     * @SWG\Header(header="item_name-project_name", type="string", description="项目名称"),
     * )
     * )
     */
    public function applic_rect_submit()
    {
        $post = request()->only([
            'id',
            'describe',
            'img',
            'item_id'
        ], 'post');
        if (empty($post['id'])) {
            err('列表id不能为空');
        }
        if (empty($post['item_id'])) {
            err('项目id不能为空');
        }
        if (! empty($post['describe'])) {
            $data['describe'] = $post['describe'];
        }
        if (! empty($post['img'])) {
            $imgs = json_encode($post['img']);
            $data['img'] = $imgs;
        }
        $data['user_id'] = $this->accessData['uid'];
        $data['item_id'] = $post['item_id'];
        $data['cid'] = $post['id'];
        $data['create_time'] = date('Y-m-d H:i:s');
        
        $res = db('wx_jg_applics')->insertGetId($data);
        if ($res) {
            // 更改安全检查状态为已整改
            db('wx_jg_application')->where([
                'id' => $post['id']
            ])->update([
                'sty' => 1
            ]);
            
            // 上传同事圈图片
            $data = [];
            $data['item_id'] = $post['item_id'];
            $data['image'] = $imgs;
            $data['speed_name'] = $post['describe'];
            $data['create_time'] = time();
            $data['type'] = 5;
            $data['type_id'] = $res;
            // 个人信息
            $will = $this->UserName($this->accessData['uid']);
            $data['user_id'] = $this->accessData['uid'];
            $data['user_name'] = $will['name'];
            $data['head_img'] = $will['avatarurl'];
            // 项目名称
            $item = $this->ProjectName($post['id']);
            $data['item_name'] = $item['project_name'];
            db('wx_jg_coworker')->insert($data);
            // 增加活跃度
            db('project')->where([
                'id' => $post['item_id']
            ])->setInc('activity_degree', 10); // 关注一位用户活跃度+10
            suc('提交成功');
        } else {
            err('提交失败');
        }
    }

    /**
     * @SWG\Post(
     * path="/applic_history",
     * tags={"Center"},
     * summary="个人中心-整改反馈-已整改详情",
     * description="个人中心-整改反馈-已整改详情-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="检查列表id", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="application-id", type="string", description="列表id"),
     * @SWG\Header(header="application-results", type="string", description="问题描述"),
     * @SWG\Header(header="application-date", type="string", description="日期"),
     * @SWG\Header(header="application-image", type="string", description="图片"),
     * @SWG\Header(header="applic-describe", type="string", description="整改描述"),
     * @SWG\Header(header="applic-img", type="string", description="整改图片"),
     * )
     * )
     */
    public function applic_history()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (empty($post['id'])) {
            err('列表id不能为空');
        }
        
        // 问题描述
        $res['application'] = db('wx_jg_application')->field('id,results,date,image')
            ->where([
            'id' => $post['id']
        ])
            ->find();
        
        $res['application']['image'] = json_decode($res['application']['image']);
        
        // 整改措施
        
        $res['applic'] = db('wx_jg_applics')->field('describe,img')
            ->where([
            'cid' => $post['id']
        ])
            ->find();
        $res['applic']['img'] = json_decode($res['applic']['img']);
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/FinanceList",
     * tags={"Center"},
     * summary="个人中心-财务-列表",
     * description="个人中心-财务-列表-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function FinanceList()
    {
        // 获取绑定的公司名称
        $res = db('wx_jg_binding')->alias('a')
            ->join('company b', 'a.code = b.code', 'left')
            ->where([
            'a.type' => 2,
            'a.user_id' => $this->accessData['uid']
        ])
            ->field('b.id,b.company_name,b.amount_all,b.payment')
            ->find();
        if (! $res) {
            err('没有数据');
        }
        suc($res);
    }

    /**
     * @SWG\Post(
     * path="/Payment",
     * tags={"Center"},
     * summary="个人中心-财务-列表提交",
     * description="个人中心-财务-列表提交-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="公司ID", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="amount_all", description="公司总合同额", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="payment", description="回款数", in="formData", required=true, type="string"),
     *
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Payment()
    {
        $post = request()->only([
            'id',
            'payment',
            'amount_all'
        ], 'post');
        if (empty($post['id'])) {
            err('公司id不能为空');
        }
        $res = db('company')->where([
            'id' => $post['id']
        ])->update([
            'payment' => $post['payment'],
            'amount_all' => $post['amount_all']
        ]);
        if (! $res) {
            err('提交失败');
        } else {
            // 更新这个公司所有项目得活跃度
            db('project')->where([
                'company_id' => $post['id']
            ])->setInc('activity_degree', 10);
            
            suc('提交成功');
        }
    }

    /**
     * @SWG\Post(
     * path="/Explain",
     * tags={"Center"},
     * summary="个人中心-使用说明",
     * description="个人中心-使用说明-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Explain()
    {
        $res['content'] = db('wx_jg_explain_content')->field('title,title1,title2,url')
            ->where("type = 1")
            ->find();
        $res['contacts'] = db('wx_jg_explain_contacts')->field('name,tel,weixin')
            ->order('create_time desc')
            ->select();
        
        if ($res) {
            suc($res);
        } else {
            err('没有数据');
        }
    }
    
    // 获取所有公司
    public function home_list()
    {
        $res = db('company')->field('id,company_name')
            ->where([
            'status' => 1
        ])
            ->order('sort asc')
            ->select();
        if ($res) {
            suc($res);
        } else {
            err('没有数据');
        }
    }
    
    // 获取相应公司的项目
    public function home_item()
    {
        $post = request()->only([
            'id'
        ], 'post');
        if (empty($post['id'])) {
            err('公司id不能为空');
        }
        $res = db('project')->field('id,project_name')
            ->where([
            'company_id' => $post['id']
        ])
            ->select();
        if ($res) {
            suc($res);
        } else {
            err('没有数据');
        }
    }
    
    // 获取个人中心所有公司
    public function home_lists()
    {
        $res = db('company')->field('id,company_name')
            ->order('sort asc')
            ->select();
        if ($res) {
            suc($res);
        } else {
            err('没有数据');
        }
    }
    
    // 项目模型图修改
    public function img_edit()
    {
        $post = request()->only([
            'id',
            'project_model'
        ], 'post');
        if (empty($post['id'])) {
            err('项目id不能为空');
        }
        if (! empty($post['project_model'])) {
            $data['project_model'] = IMG_ADMIN . $post['project_model'];
        }
        $res = db('project')->where([
            'id' => $post['id']
        ])->update($data);
        if (! $res) {
            err('修改失败');
        } else {
            suc('修改成功');
        }
    }

    /**
     * @SWG\Post(
     * path="/province",
     * tags={"Region"},
     * summary="省级地址查询",
     * description="省级地址查询-夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    // 省级地址查询
    public function province()
    {
        $province = db('area')->where("pid='0'")->select();
        if ($province) {
            suc('', $province);
        } else {
            err('', '无省级地址');
        }
    }

    /**
     * @SWG\Post(
     * path="/city",
     * tags={"Region"},
     * summary="市级地址查询",
     * description="市级地址查询-夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="province_id", description="省级地址id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    // 市级地址查询
    public function city()
    {
        $province_id = input('province_id'); // 省级地址id
        $city = db('area')->where("pid='{$province_id}'")->select();
        if ($city) {
            suc('', $city);
        } else {
            err('', '无市级地址');
        }
    }

    /**
     * @SWG\Post(
     * path="/district",
     * tags={"Region"},
     * summary="区级地址查询",
     * description="区级地址查询-夏亲",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="city_id", description="市级地址id", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    // 区级地址查询
    public function district($city_id)
    {
        $city_id = input('city_id'); // 市级地址id
        $district = db('area')->where("pid='{$city_id}'")->select();
        if ($district) {
            suc('', $district);
        } else {
            err('', '无区级地址');
        }
    }
    
    // 公司项目名搬迁
    public function removal()
    {
        // 获取所有已审核项目
        $project = db('project')->where('review_status = 1')->select();
        foreach ($project as $value) {
            // 获取项目公司名
            $company_name = db('company')->where("id='{$value['company_id']}'")->value('company_name');
            // 更新公司名
            db('project')->where("id='{$value['id']}'")->update([
                'item_name' => $company_name
            ]);
        }
        
        suc('更新成功');
    }
    
    // 状态更新
    public function sta()
    {
        $project = db('project')->field('id,status')->select();
        // $project = db('project')->where('status=1')->count();
        // dump($project);die();
        foreach ($project as $value) {
            if ($value['status'] == '-1') {
                $value['status'] = 0;
            }
            // 更新状态更新
            db('project')->where("id='{$value['id']}'")->update([
                'review_status' => $value['status']
            ]);
        }
        suc('更新成功');
    }
}

